Skip to content

Instantly share code, notes, and snippets.

@sohail-aspose
Created December 15, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohail-aspose/b076d3dc992b261c228f2f02a758f638 to your computer and use it in GitHub Desktop.
Save sohail-aspose/b076d3dc992b261c228f2f02a758f638 to your computer and use it in GitHub Desktop.
Merge JPG Images and Save them as PNG
// An array of images you want to merge.
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" };
string outputPath = "MergeHorizontalAsPNG";
// 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 resulting image.
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);
// Merging images into one and save as PNG
using (MemoryStream memoryStream = new MemoryStream())
{
StreamSource outputStreamSource = new StreamSource(memoryStream);
JpegOptions options = new JpegOptions() { Source = outputStreamSource, 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 + ".png", new PngOptions());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment