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