Asp.net image quality compression (without changing the size ),

Source: Internet
Author: User

Asp.net image quality compression (without changing the size ),


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;
}

/// <Summary>
/// Image compression (reduce the quality to reduce the file size)
/// </Summary>
/// <Param name = "srcBitmap"> incoming Bitmap object </param>
/// <Param name = "destStream"> compressed Stream object </param>
/// <Param name = "level"> compression level, 0 to 100, 0 worst quality, best </param>
Public static void Compress (Bitmap srcBitmap, Stream destStream, long level)
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

// Get an ImageCodecInfo object that represents the JPEG codec.
MyImageCodecInfo = GetEncoderInfo ("image/jpeg ");

// Create an Encoder object based on the GUID

// For the Quality parameter category.
MyEncoder = 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.
MyEncoderParameters = new EncoderParameters (1 );

// Save the bitmap as a JPEG file with the given quality level
MyEncoderParameter = new EncoderParameter (myEncoder, level );
MyEncoderParameters. Param [0] = myEncoderParameter;
SrcBitmap. Save (destStream, myImageCodecInfo, myEncoderParameters );
}

/// <Summary>
/// Image compression (reduce the quality to reduce the file size)
/// </Summary>
/// <Param name = "srcBitMap"> incoming Bitmap object </param>
/// <Param name = "destFile"> path of the compressed image </param>
/// <Param name = "level"> compression level, 0 to 100, 0 worst quality, best </param>
Public static void Compress (Bitmap srcBitMap, string destFile, long level)
{
Stream s = new FileStream (destFile, FileMode. Create );
Compress (srcBitMap, s, level );
S. Close ();
}

 

Related Article

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.