Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 11, 2022 16:34

Aspose.Imaging .NET API allows to easily merge images in your .NET application or Web service.

You can:

  • Merge jpg,png,bmp,pdf and other formats

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.jpg";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combining images into new 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);
}
}
File.Delete(outputPath);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.jpg";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);
// Combining images into new 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);
}
File.Delete(outputPath);
File.Delete(tempFilePath);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.pdf";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combining images into new 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, new PdfOptions());
}
}
File.Delete(outputPath);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.pdf";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);
// Combining images into new 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, new PdfOptions());
}
File.Delete(outputPath);
File.Delete(tempFilePath);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.png";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combining images into new 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 + ".png", new PngOptions());
}
}
File.Delete(outputPath);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.png";
string tempFilePath = dataDir + "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);
}
}
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);
// Combining images into new 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, new PngOptions());
}
File.Delete(outputPath);
File.Delete(tempFilePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment