Working with multipage image formats

Aspose.Imaging supports rich functionality to work with multipage image formats such as Gif, Tiff, Psd, Dicom, Cdr, WebP etc. Also, using Aspose.Imaging image can be exported also to multipage PDF document.

To indicate whether the image contains layers/pages/ frames the IMultipageImage interface has been introduced. All multi-page images such as PSD, CDR, GIF, etc. are descendants of this interface. Using Pages property, you can access the pages of any multi-page image in the library.

Example of usage IMultipageImage interface:

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;
}
}

Export of multipage images can be performed using MultiPageOptions class. With this option you can specify the pages that you want to export to another format. In the case of export to a single-page format, the 1st page of the range will be exported; in the case of export to a multi-page format, all pages of the range will be exported.

Example of export from multi-page format to single-page image format:

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");

Example of export from multi-page format to multi-page

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");

Here the 4th and 5th pages will be exported to the tiff format

Below presented example of export from/to different multipage image formats:

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);
}
}

Support of making gifs and other multi-page (multi-frame) files from set of images

Create multipage images using AddPage method

You can create multipage image using AddPage() method. The following code shows how you can create animated images using image frames from the folder:

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);
}
}

Create multipage image from vector images

In order to use vector images as animation frames you need to rasterize them first. The following source code sample shows how to create TIFF image using vector images:

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);
}
}

Create animation from an array of images

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");