Convert tiff image byte array to png image byte array

Convert tiff image byte array to png image byte array

Issue : Convert tiff image byte array to png image byte array.

Tips : To convert tiff image byte array to png image byte array there can be used saving bytes from/to MemoryStream.

Example :

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (TiffImage image = (TiffImage)Image.Load(dataDir + "template.tiff"))
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms);
byte[] bytes = TiffToPng(ms.ToArray());
}
}
byte[] TiffToPng(byte[] tiffBytes)
{
// create stream from TIFF byte array
using (var tiffStream = new MemoryStream(tiffBytes))
{
// load TIFF image
using (var tiffImage = Image.Load(tiffStream))
{
// create emtpy output stream
using (var pngStream = new MemoryStream())
{
// create and customize PNG options
var pngOptions = new PngOptions
{
// use PngColorType.TruecolorWithAlpha to get highest image color depth and keep transparency transparency of input image
ColorType = PngColorType.TruecolorWithAlpha,
// set compression level from 0 to 9 (by default it is set to 6; higher value - less size)
CompressionLevel = 9,
};
// export TIFF according to specified options
tiffImage.Save(pngStream, pngOptions);
// return PNG byte array
return pngStream.ToArray();
}
}
}
}