Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to compress PNG image output just be in png

I am using below code: (Below code is working fine for jpeg,jpg) but not for png.

What I have tried:

C#
private void VaryQualityLevel()
{
    // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

    // Create an Encoder object based on the GUID
    // for the Quality parameter category.
    System.Drawing.Imaging.Encoder myEncoder =
        System.Drawing.Imaging.Encoder.Quality;

    // Create an EncoderParameters object.
    // An EncoderParameters object has an array of EncoderParameter
    // objects. In this case, there is only one
    // EncoderParameter object in the array.
    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
}

private ImageCodecInfo GetEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}
Posted
Updated 16-Feb-16 2:32am
v2

1 solution

JPEG uses lossy compression while Portable Network Graphics - Wikipedia, the free encyclopedia[^] uses a lossless compression (DEFLATE algorithm). Therefore, it has no quality parameter (quality is always 100 %).

However, the size of PNG images may be optimised by some methods like using a palette of colours instead of RGB values. But such operations are not supported by the .NET classes. See also the output at the end of Listing Parameters and Values for All Encoders (Windows)[^].
 
Share this answer
 
Comments
Khurram Mumtaz 17-Feb-16 0:32am    
All i want to reduce a size of image. I see some softwares those are doing this but don't know how
Jochen Arndt 17-Feb-16 3:09am    
When working with images on computers, they are processed as bitmaps. The size of memory required to hold a bitmap can be only reduced by reducing the resolution and/or the color depth. Both methods result in loss of information (irreversible process).

To created a scaled .NET Bitmap, use the Bitmap(Image, Int32, Int32) or Bitmap(Image, Size) constructor. Then you can save the bitmap using the image file format of your choice.
Khurram Mumtaz 17-Feb-16 3:18am    
I already know this. Can you provide me a sample code
Jochen Arndt 17-Feb-16 3:32am    
Add this to your code after determining the required width and height (e.g. half the sizes of your bmp1):

Bitmap bmp2 = new Bitmap(bmp1, width, height);

Then save bmp2 in the format of your choice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900