.NET Code sample to convert Tiff image byte array to png image byte array

Hi,

We want to convert Tiff byte array to Png byte array using Aspose components.
Can you please share the code samples with different customization options for compression, color depth etc.
We need to maintain the original quality with reduced file size

Thanks,
Govardhana Reddy.

Hello,

in order to convert TIFF byte array to PNG byte array you can look through an example, which, I hope, is going to be useful:

public static 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();
            }
        }
    }
}

Hi Denis,
Thank you for the code :slightly_smiling_face:

With your code we are converting Tiff byte array to Png byte with PngColorType=Grayscale and CompressionLevel = 9 options.
TIFF Original images size is 22.2 MB and converted PNG images size is 7.95 MB. Is there any other options there to reduce more size with images retain the quality ?.

Can you please give me information below Aspose.Imaging.ImageOptions.PngOptions

  1. BitDepth
  2. PngFilterType
  3. Progressive
  4. XmpPacketWrapper

Thanks,
Govardhana Reddy Renati.

@C5134314
Please review following links to get an ideas on use of Aspose.Imaging.ImageOptions.PngOptions:

https://reference.aspose.com/imaging/net/aspose.imaging.imageoptions/pngoptions/progressive/

https://reference.aspose.com/imaging/net/aspose.imaging.imageoptions/pngoptions/filtertype/

Hi ,

Thank you for your response.
Converting Tiff byte array to Png byte with PngColorType=Grayscale and CompressionLevel = 9 options the quality is good but size is slightly high .
We want reduce more size with images retain the quality(Manageable quality) so can please suggest which following options is best and give me more information below

Aspose.Imaging.ImageOptions.PngOptions.PngFilterType enumeration

  1. None
  2. Sub
  3. Up
  4. Avg
  5. Paeth
  6. Adaptive

Thanks,
Govardhana Reddy Renati

Hello,

I would say there is no universal rule for PngFilterType pick. Each filter uses its own approach and gives different results depending on pixel data. PngFiltertype.Paeth has the most complicated implementation among the rest, although, does not always grant the least output size.
In order to get the least output image size for sure, You should pick PngFiltertype.Adaptive, but it takes more time for calculations. Progressive option may also reduce output size combined with filter usage.
Try to run conversion with different PngOptions, so you will clearly see the output size difference.