Re: Jpeg Compression Ratio

Efran Cobisi <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Organization cobisi.com
Message-ID <[email protected]>
Hi Simon,

> If I have a System.Drawing.Bitmap instance and want to save it as a jpg file, does anyone happen to know if there's a way to control the compression ratio it gets saved as? 

First of all you need to retrieve the encoder for the Jpeg image format:

---------------
ImageCodecInfo jpegCodec = null;

// Retrieve the codec for the jpeg format

foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
{
    if (codec.FormatID == ImageFormat.Jpeg.Guid)
    {
        jpegCodec = codec;
        break;
    }
}

if (jpegCodec == null)
    throw new NotSupportedException("...");
---------------

Then you should configure the encoder with the settings you need.
Should you need a 50% quality (compression) factor output, for example, 
you can use the following code:

---------------
// Defines the parameters the encoder will use (quality factor

EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 50);
---------------

After that, the codec and its parameters can be used to (re)save the 
original bitmap:

---------------
Bitmap bmp = ...;
bmp.Save("...", jpegCodec, encoderParameters);
---------------

HTH

--
Efran Cobisi
http://www.cobisi.com

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.