Determine the JPEG quality factor by using Visual C# .NET

Article translations Article translations
Article ID: 324790 - View products that this article applies to.
This article was previously published under Q324790
For a Microsoft Visual Basic .NET version of this article, see 324788.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Drawing.Imaging
Expand all | Collapse all

On This Page

SUMMARY

Compression in a Joint Photographic Experts Group (JPEG) file is controlled by a set of quantization tables. Typically, there are two such tables in each file: one for the luminance (brightness) information and the other for the chrominance (color) information. These tables are 8x8 matrices that determine how the 8x8 blocks of discrete cosine coefficients are quantized.

Most applications allow you to specify a number, called the JPEG quality factor, to control the compression level. This number typically has a range from 0 or 1, which results in more compression and a smaller file, to 100, which results in almost no compression and a larger file. However, this number is not stored in the file.

The JPEG quality factor is used to generate a pair of quantization tables. Many applications that are based on the Independent JPEG Group (IJG) code generate the same quantization tables as the IJG reference code. However, some applications may use custom quantization tables instead.

For more information about how the quality factor is used to influence the quantization tables, see the JPEG specification and the IJG reference code, both of which are available through the "References" section of this article.

MORE INFORMATION

Set the JPEG Quality Factor

The JPEG encoder in System.Drawing gives an encoder parameter for setting the JPEG quality factor when you save a JPEG file. This encoder parameter conforms to the convention of using an integer with a range of 1 to 100. The value of this encoder parameter influences the creation and use of quantization tables related to the suggested quality factor.

The following code sample shows how this encoder parameter is used to save a JPEG file with a specified quality factor:
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
    int j;
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for(j = 0; j < encoders.Length; ++j)
    {
        if(encoders[j].MimeType == mimeType)
            return encoders[j];
    }
    return null;
}
private void SaveJPGWithCompressionSetting( Image image, string szFileName, long lCompression )
{
    EncoderParameters eps = new EncoderParameters(1);
    eps.Param[0] = new EncoderParameter( Encoder.Quality, lCompression );
    ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
    image.Save( szFileName, ici, eps );
}
				

Retrieve the Quality Factor from a JPEG File

The quality factor is not stored directly in the JPEG file, so you cannot read the quality factor from the file. However, you can read the quantization tables from the JPEG file by using the PropertyItems property on the Image class. But even with the quantization tables, you cannot always determine a quality factor.

You might be able to determine the quality factor by comparing the quantization tables against the "standard" IJG-generated tables. However, because some applications may use custom tables, you will not always find a match. For more information about the quantization tables, see the "References" section.

REFERENCES

For more information, visit the following organization Web sites:
Independent JPEG Group

Properties

Article ID: 324790 - Last Review: May 10, 2006 - Revision: 2.4
APPLIES TO
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
Keywords: 
kbhowtomaster kbinfo kbdswgdi2003swept KB324790

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com