C # Image Processing: Save As a jpeg file that can be controlled by the compression quality

Source: Internet
Author: User

The common process for processing images is: Read the image file and convert it to Bitmap-> process each point of the bitmap to get the desired effect-> Save the new bitmap to the file
Using C #, you can easily read image files in multiple formats to bitmap objects. One sentence is enough. Common formats are supported, such as JPEG, BMP, and PNG.

Bitmap BMP = new Bitmap ("file name ");

Then there is the question of how to handle this image. It has nothing to do with this case. Pass.

The last step is save. Although JPEG is a lossy compression solution, it finds a balance between reducing the file size and retaining the original information as much as possible. Therefore, JPEG is the preferred storage solution in many cases.

C # certainly won't ignore this. The bitmap class provides the default save as JPEG method:

BMP. Save ("output file", system. Drawing. imaging. imageformat. JPEG );

This is of course very convenient, but sometimes it is more important to care about the file size and sometimes the image quality. Is there any way to control the compression quality?

The answer is yes. The encoderparameters parameter is used in the reload of the BMP. Save method. We can add our own quality control in this parameter.
/** // <Summary>
/// Used to save JPG files
/// </Summary>
/// <Param name = "mimetype"> </param>
/// <Returns> obtain the imagecodecinfo of the specified mimetype. </returns>
Private Static imagecodecinfo getcodecinfo (string mimetype)
{
Imagecodecinfo [] codecinfo = imagecodecinfo. getimageencoders ();
Foreach (imagecodecinfo ICI in codecinfo)
{
If (ICI. mimetype = mimetype) return ICI;
}
Return NULL;
}

/** // <Summary>
/// Save as JPEG and support Compression Quality options
/// </Summary>
/// <Param name = "BMP"> </param>
/// <Param name = "FILENAME"> </param>
/// <Param name = "QTY"> </param>
/// <Returns> </returns>
Public static bool kisaveasjpeg (bitmap BMP, string filename, int qty)
{
Try
{
Encoderparameter P;
Encoderparameters pS;

PS = new encoderparameters (1 );

P = new encoderparameter (system. Drawing. imaging. encoder. Quality, qty );
PS. Param [0] = P;

BMP. Save (filename, getcodecinfo ("image/JPEG"), PS );

Return true;
}
Catch
{
Return false;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.