Reputation: 6500
I'm using the following code to save image in different formats
public void saveJpeg(string path, Bitmap img, long quality)
{
System.Drawing.Imaging.EncoderParameter qualityParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
System.Drawing.Imaging.ImageCodecInfo Codec = this.getEncoderInfo(imgformat);
if (Codec == null)
return;
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path + ext, Codec, encoderParams);
}
The quality setting works fine in case of JPEG Images.But when i set the quality to 20 and 100 for .bmp and .png formats i get files with the same size as output. What is going wrong here? Please advice.
Upvotes: 1
Views: 1728
Reputation: 70671
"What is going wrong here?"
Nothing is going wrong here. The quality setting applies only to the JPEG encoder. PNG and BMP are both lossless, meaning that there's no quality/space trade-off to be made. You will always get exactly the same pixels out from those formats as went in.
Unfortunately, .NET does not offer much in the way of compression options for those formats, in any of its image-handling APIs. BMP does support a "run-length encoding" format, but to my knowledge there's no .NET API that will actually generate it. Likewise PNG, which actually has a lot more options that can be selected to tailor the compression used to the nature of the input image and the amount of time one wants to spend compressing the file.
But even if you had access to those options, they wouldn't affect the quality of the image. Just the final compressed size of the file (e.g. for PNG using the right compression algorithm and/or spending more time compressing the file will result in a smaller file, but still a file that is exactly the same as the image you started with).
Upvotes: 3