Created
December 15, 2020 06:03
-
-
Save sohail-aspose/e6cc60445354e0f7dd945f1e113ebf40 to your computer and use it in GitHub Desktop.
Merge JPEG Images Horizontally in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array of Images you want to merge. | |
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; | |
string outputPath = "MergedJPEGImages_Horizontally.jpg"; | |
string tempFilePath = "temp.jpg"; | |
// Getting resulting image size. | |
List<Size> imageSizes = new List<Size>(); | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
imageSizes.Add(image.Size); | |
} | |
} | |
// Width and Height of the resultant image. | |
int newWidth = imageSizes.Sum(size => size.Width); | |
int newHeight = imageSizes.Max(size => size.Height); | |
// Merging JPEG images into one. | |
Source tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true); | |
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 }; | |
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) | |
{ | |
int stitchedWidth = 0; | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height); | |
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); | |
stitchedWidth += image.Width; | |
} | |
} | |
newImage.Save(outputPath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment