NewCoder
NewCoder

Reputation: 21

c# When converting TIFF to PNG how do I get the same compress I get by using MS Paint

This is the code I'm using to convert the TIFF to PNG.

var image = Image.FromFile(@"Test.tiff");

var encoders = ImageCodecInfo.GetImageEncoders();
var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == "image/tiff");

if (imageCodecInfo == null)
{
   return;
}

var imageEncoderParams = new EncoderParameters(1);
imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
image.Save(@"Test.png", imageCodecInfo, imageEncoderParams);

The TIFF file size is 46.8 MB (49,161,628 bytes) the PNG that is made using this code is 46.8 MB (49,081,870 bytes) but if I use MS paint the PNG file size is 6.69 MB (7,021,160 bytes).

So what do I change in the code to get the same compress I get by using MS Paint?

Upvotes: 1

Views: 930

Answers (2)

NewCoder
NewCoder

Reputation: 21

OK, after a lot of trial and error I came up with this.

                var image = Image.FromFile(@"Test.tiff");
                Bitmap bm = null;
                PictureBox pb = null;

                pb = new PictureBox();
                pb.Size = new Size(image.Width, image.Height);
                pb.Image = image;
                bm = new Bitmap(image.Width, image.Height);
                ImageCodecInfo png = GetEncoder(ImageFormat.Png);
                EncoderParameters imageEncoderParams = new EncoderParameters(1);
                imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                pb.DrawToBitmap(bm, pb.ClientRectangle);
                bm.Save(@"Test.png", png, encodePars);
                pb.Dispose();

And add this to my code.

            private ImageCodecInfo GetEncoder(ImageFormat format)
            {
               ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
               foreach (ImageCodecInfo codec in codecs)
               if (codec.FormatID == format.Guid)
               return codec;
               return null;
             }

By loading the TIFF in a PictureBox then saving it as a PNG the output PNG file size is 7.64 MB (8,012,608 bytes). Witch is a little larger then Paint But that is fine.

Upvotes: 0

Peter Duniho
Peter Duniho

Reputation: 70701

Without a good Minimal, Complete, and Verifiable code example, it's impossible to know for sure. But…

The code you posted appears to be getting a TIFF encoder, not a PNG encoder. Just because you name the file with a ".png" extension does not mean that you will get a PNG file. It's the encoder that determines the actual file format.

And it makes perfect sense that if you use the TIFF encoder, you're going to get a file that's exactly the same size as the TIFF file you started with.

Instead, try:

var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == "image/png");

Note that this may or may not get you exactly the same compression used by Paint. PNG has a wide variety of compression "knobs" to adjust the exact way it compresses, and you don't get access to most of those through the .NET API. Paint may or may not be using the same values as your .NET program. But you should at least get a similar level of compression.

Upvotes: 1

Related Questions