Created
December 15, 2020 08:10
-
-
Save sohail-aspose/2d46d079a283dc028ed2eae270ac6e75 to your computer and use it in GitHub Desktop.
Merge JPEG Images Vertically 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
// An array of images you want to merge | |
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; | |
string outputPath = "MergeImages_Vertically.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 resulting image | |
int newWidth = imageSizes.Max(size => size.Width); | |
int newHeight = imageSizes.Sum(size => size.Height); | |
// Merging images into one. | |
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 stitchedHeight = 0; | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
Rectangle bounds = new Rectangle(0, stitchedHeight, image.Width, image.Height); | |
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); | |
stitchedHeight += image.Height; | |
} | |
} | |
newImage.Save(outputPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment