- 使用 C# 合并多个图像
为了将多个图像合并为一个图像,我们将使用Aspose.Imaging,它是一个强大且功能丰富的图像处理 API,可让您处理各种图像格式。还没使用过的朋友可以最新版

使用 C# 合并多个图像
有两种方法可以将图像合并为一个:垂直和水平。在前一种方法中,图像在垂直方向上相互附加,而在后一种方法中,图像在水平方向上一个接一个地组合。
垂直合并图像
以下是使用 C# 垂直合并图像的步骤。
- 首先,在字符串数组中指定图像的路径。
- 然后,创建一个大小列表并将每个图像的大小存储到其中。
- 计算结果图像的高度和宽度。
- 创建StreamSource类的对象并使用新的MemoryStream对其进行初始化。
- 创建JpegOptions类的对象并设置其选项。
- 为新图像实例化JpegImage类,并使用JpegOptions和计算的高度和宽度对其进行初始化。
- 遍历图像列表,并在每次迭代中将图像加载到RasterImage对象中。
- 为每个图像创建一个矩形,并使用JpegImage.SaveArgb32Pixels()方法将其添加到新图像中。
- 在每次迭代中增加缝合高度。
- 最后,使用JpegImage.Save(string)方法保存新图像。
以下代码示例展示了如何垂直合并图像。
// Create a list of imagesstring[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.png" };// Get resulting image's sizeListimageSizes = new List();foreach (string imagePath in imagePaths){ using (RasterImage image = (RasterImage)Image.Load(imagePath)) { imageSizes.Add(image.Size); }}int newWidth = imageSizes.Max(size => size.Width);int newHeight = imageSizes.Sum(size => size.Height);// Combine images into new oneusing (MemoryStream memoryStream = new MemoryStream()){ // Create output source StreamSource outputStreamSource = new StreamSource(memoryStream); // Create jpeg options JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 }; // Create output image using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) { int stitchedHeight = 0; // Merge images 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; } } // Save the merged image newImage.Save("merged-image.jpg"); }}
水平合并图像
以下是使用 C# 水平组合图像的步骤。
- 首先,在字符串数组中指定图像的路径。
- 然后,创建一个大小列表并将每个图像的大小存储到其中。
- 计算结果图像的高度和宽度。
- 使用FileCreateSource(String, Boolean)创建一个新源并使用文件路径对其进行初始化。
- 创建JpegOptions类的对象并设置其选项。
- 为新图像实例化JpegImage类,并使用JpegOptions和计算的高度和宽度对其进行初始化。
- 遍历图像列表,并在每次迭代中将图像加载到RasterImage对象中。
- 为每个图像创建一个矩形,并使用JpegImage.SaveArgb32Pixels()方法将其添加到新图像中。
- 在每次迭代中增加缝合宽度。
- 完成后,使用JpegImage.Save(string)方法保存新图像。
以下代码示例展示了如何水平合并多个图像。
// Create list of imagesstring[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.JPG", "image5.png" };// To create a temporary imagestring tempFilePath = "temp.jpg";// Get resulting image's sizeList <Size> imageSizes = new List<Size>();foreach (string imagePath in imagePaths){ using (RasterImage image = (RasterImage)Image.Load(imagePath)) { imageSizes.Add(image.Size); }}int newWidth = imageSizes.Sum(size => size.Width);int newHeight = imageSizes.Max(size => size.Height);// Combine images into new oneSource tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true);// Create jpeg optionsJpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 };using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)){ int stitchedWidth = 0; // Merge images 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; } } // Save the merged image newImage.Save(outputPath);}
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
如果您有任何疑问或需求,请随时加入Aspose技术交流群(),很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!