Skip to content

Instantly share code, notes, and snippets.

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 aspose-com-gists/af09b6dfc586ad81f2bc7806eff52a20 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/af09b6dfc586ad81f2bc7806eff52a20 to your computer and use it in GitHub Desktop.

Aspose.Imaging .NET API allows to easily work with multipage images in your .NET application or Web service.

You can:

  • Make export of multipage images;
  • Create multipage image from vector images;
  • Support of making gifs and other multi-page (multi-frame) files from set of images;
  • Create animation from an array of images.

Interested ?

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

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Apng;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] files = new string[]{ "template.tiff", "template.gif", "template.png" };
List<Image> images = new List<Image>();
foreach (var file in files)
{
string filePath = Path.Combine(dataDir, file);
images.Add(Image.Load(filePath));
}
using (Image image = Image.Create(images.ToArray(), true))
{
image.Save(dataDir + "result.tiff", new TiffOptions(TiffExpectedFormat.TiffJpegRgb));
}
foreach (Image image in images)
{
image.Dispose();
}
File.Delete(dataDir + "result.tiff");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Apng;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
MultipageFromVector();
File.Delete(dataDir + "result.tiff");
File.Delete(dataDir + "result.png");
File.Delete(dataDir + "result2.png");
File.Delete(dataDir + "result3.png");
void MultipageFromVector()
{
// Rasterize vector images
RasterizeSvgToPng(dataDir + "template.svg", dataDir + "result.png");
RasterizeSvgToPng(dataDir + "template.svg", dataDir + "result2.png");
RasterizeSvgToPng(dataDir + "template.svg", dataDir + "result3.png");
// Load frames
var frames = LoadFrames().ToArray();
// Create TIFF image using the first frame
using (var image = new TiffImage(new TiffFrame(frames[0])))
{
// Add frames to the TIFF image using the AddPage method
for (var index = 1; index < frames.Length; index++)
{
image.AddPage(frames[index]);
}
// Save TIFF image using options
var options = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
image.Save(dataDir + "result.tiff", options);
}
foreach (RasterImage image in frames)
{
image.Dispose();
}
}
void RasterizeSvgToPng(string inputPath, string outputPath)
{
// Load vector image
using (var image = Image.Load(inputPath))
{
// Save PNG image
image.Save(outputPath, new PngOptions
{
// Create rasterization options
VectorRasterizationOptions = new SvgRasterizationOptions
{
PageWidth = image.Width,
PageHeight = image.Height
}
});
}
}
IEnumerable<RasterImage> LoadFrames()
{
foreach (var filePath in new string[] { "result.png", "result2.png", "result3.png" })
{
yield return (RasterImage)Image.Load(dataDir + filePath);
}
}
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Apng;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load frames
var frames = LoadFrames().ToArray();
// Create TIFF image using the first frame
using (var image = new TiffImage(new TiffFrame(frames[0])))
{
// Add frames to the TIFF image using the AddPage method
for (var index = 1; index < frames.Length; index++)
{
image.AddPage(frames[index]);
}
// Save TIFF image using options
var options = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
image.Save(dataDir + "result.tiff", options);
File.Delete(dataDir + "result.tiff");
}
// Create WEBP image using the first frame
using (var image = new WebPImage(frames[0]))
{
// Add frames to the WEBP image using the AddPage method
for (var index = 1; index < frames.Length; index++)
{
image.AddPage(frames[index]);
}
// Save WEBP image
image.Save(dataDir + "result.webp");
File.Delete(dataDir + "result.webp");
}
// Determine frame size
var framesize = frames[0].Size;
// Create APNG image
using (var image = new ApngImage(new ApngOptions(), framesize.Width, framesize.Height))
{
// Add frames to the APNG image using the AddPage method
for (var index = 0; index < frames.Length; index++)
{
image.AddPage(frames[index]);
}
// Save APNG image
image.Save(dataDir + "result.png");
File.Delete(dataDir + "result.png");
}
// Determine frame size
framesize = frames[0].Size;
// Create DICOM image
using (var image = new DicomImage(new DicomOptions(), framesize.Width, framesize.Height))
{
// Add frames to the APNG image using the AddPage method
for (var index = 0; index < frames.Length; index++)
{
image.AddPage(frames[index]);
}
// Remove default empty page
image.RemovePage(0);
// Save DICOM image
image.Save(dataDir + "result.dcm");
File.Delete(dataDir + "result.dcm");
}
foreach (RasterImage image in frames)
{
image.Dispose();
}
IEnumerable<RasterImage> LoadFrames()
{
foreach (var filePath in new string[] { "template.png", "template.tiff", "template.bmp" })
{
yield return (RasterImage)Image.Load(dataDir + filePath);
}
}
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
int startPage = 0;
int countPage = 1;
using (Image image = Image.Load(dataDir + "template.tiff"))
{
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffDeflateRgb);
tiffOptions.MultiPageOptions = new MultiPageOptions(new IntRange(startPage, countPage));
image.Save(dataDir + "result.tiff", tiffOptions);
}
File.Delete(dataDir + "result.tiff");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Code for using ExportImage method
ImageOptionsBase[] imageOptions = new ImageOptionsBase[] { new WebPOptions(), new GifOptions(),
new TiffOptions(TiffExpectedFormat.Default), new BmpOptions(), new JpegOptions(),
new Jpeg2000Options(), new PngOptions(),
new EmfOptions(), new SvgOptions(), new WmfOptions(), new PdfOptions(),
};
string[] imageExt = new string[] { ".webp", ".gif", ".tiff", ".bmp", ".jpeg", ".j2k", ".png", ".emf", ".svg", ".wmf",".pdf" };
if (imageOptions.Length != imageExt.Length)
{
throw new Exception("imageOptions length not equal imageExt length");
}
for (int i = 0; i < imageOptions.Length; i++)
{
ExportImage(imageOptions[i], imageExt[i]);
}
void ExportImage(ImageOptionsBase imageOptions, string ext)
{
using (Image image = Image.Load(dataDir + "template.tiff"))
{
//export only 1 pages
if (image is IMultipageImage && ((IMultipageImage)image).Pages != null && ((IMultipageImage)image).PageCount > 2)
{
imageOptions.MultiPageOptions = new MultiPageOptions(new IntRange(0, 1));
}
else
{
imageOptions.MultiPageOptions = null;
}
if (image is VectorImage)
{
imageOptions.VectorRasterizationOptions = (VectorRasterizationOptions)image.GetDefaultOptions(new object[] { Color.White, image.Width, image.Height });
imageOptions.VectorRasterizationOptions.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
imageOptions.VectorRasterizationOptions.SmoothingMode = SmoothingMode.None;
}
string outFileName = Path.Combine(dataDir, "result" + ext);
image.Save(outFileName, imageOptions);
File.Delete(outFileName);
}
}
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
int startPage = 0;
int countPage = 1;
using (Image image = Image.Load(dataDir + "template.tiff"))
{
PngOptions pngOptions = new PngOptions();
pngOptions.MultiPageOptions = new MultiPageOptions(new IntRange(startPage, countPage));
image.Save(dataDir + "result.png", pngOptions);
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (Image image = Image.Load(dataDir + "template.tiff"))
{
if (image is IMultipageImage)
{
var pages = ((IMultipageImage)image).Pages;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment