• 0

[C#] Image compression


Question

I've got a small bit of code that creates thumbnails of images by using the GetThumbnailImage method. The trouble is the resulting PNG files are larger than I would like. I know that the file size can be reduced because I can re-save them through an image program which creates a smaller PNG image, trouble is I can't see a way to do this in C#, does anyone know?

Link to comment
https://www.neowin.net/forum/topic/135523-c-image-compression/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You can try to save it into a JPG using this code:

http://www.dotnet247.com/247reference/msgs/21/109227.aspx

(look for savejpgwithcompressionsetting)

I'm not sure if you can use the same piece of code to adjust the compression level (for PNG) but you can give it a try and see if it works.

  • 0

Cheers for the heads up. Unfortuantly I couldn't get any of the encoder parameters to work with a PNG. It seems that they only work with jpg and Tiff images. So instead I came up with the following, which does work nicely by reducing the number of colours in the image, though still not as nice as it could be as .Net still saves the image with 23bpp :(

But at least it's a start

Bitmap bmpFinalImage;
Rectangle rect = new Rectangle( 0, 0, 128, 128 );
PixelFormat pf = new PixelFormat.Format16bppRGB255;
bmpFinalImage = bmpOriginal.Clone( rect, pf );
bmpFinalImage.Save( @"C:\thumbnail.png", ImageFormat.Png );

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.