In web applications, the images submitted by the client must be compressed. Especially for relatively large images, if not compressed, the page will become very large and the opening speed will be slow. Of course, if you need high-quality images, you must also produce thumbnails.
The following is my own image compression algorithm. First, this is a simple unoptimized implementation:
Copy codeThe Code is as follows: public static System. Drawing. Image GetImageThumb (System. Drawing. Image sourceImg, int width, int height)
{
System. Drawing. Image targetImg = new System. Drawing. Bitmap (width, height );
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (targetImg ))
{
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G. CompositingQuality = System. Drawing. Drawing2D. CompositingQuality. HighQuality;
G. PixelOffsetMode = System. Drawing. Drawing2D. PixelOffsetMode. HighQuality;
G. drawImage (sourceImg, new System. drawing. rectangle (0, 0, width, height), new System. drawing. rectangle (0, 0, sourceImg. width, sourceImg. height), System. drawing. graphicsUnit. pixel );
G. Dispose ();
}
Return targetImg;
}
This method is relatively simple and uses high-quality compression. After compression, K images can only be compressed to about K. After code rewriting, the following methods are implemented:
Copy codeThe Code is as follows: public Bitmap GetImageThumb (Bitmap mg, Size newSize)
{
Double ratio = 0d;
Double myThumbWidth = 0d;
Double myThumbHeight = 0d;
Int x = 0;
Int y = 0;
Bitmap bp;
If (mg. Width/Convert. ToDouble (newSize. Width)> (mg. Height/
Convert. ToDouble (newSize. Height )))
Ratio = Convert. ToDouble (mg. Width)/Convert. ToDouble (newSize. Width );
Else
Ratio = Convert. ToDouble (mg. Height)/Convert. ToDouble (newSize. Height );
MyThumbHeight = Math. Ceiling (mg. Height/ratio );
MyThumbWidth = Math. Ceiling (mg. Width/ratio );
Size thumbSize = new Size (int) newSize. Width, (int) newSize. Height );
Bp = new Bitmap (newSize. Width, newSize. Height );
X = (newSize. Width-thumbSize. Width)/2;
Y = (newSize. Height-thumbSize. Height );
System. Drawing. Graphics g = Graphics. FromImage (bp );
G. SmoothingMode = SmoothingMode. HighQuality;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. PixelOffsetMode = PixelOffsetMode. HighQuality;
Rectangle rect = new Rectangle (x, y, thumbSize. Width, thumbSize. Height );
G. DrawImage (mg, rect, 0, 0, mg. Width, mg. Height, GraphicsUnit. Pixel );
Return bp;
}
This compression greatly increases the compression ratio. In fact, the Code has not changed much. The most important thing is to use jpg format when saving it. If no format is specified, png format is used by default.
The following describes how to compress an image by setting the image quality value:
Copy codeThe Code is as follows: public static bool GetPicThumbnail (string sFile, string outPath, int flag)
{
System. Drawing. Image iSource = System. Drawing. Image. FromFile (sFile );
ImageFormat tFormat = iSource. RawFormat;
// The following Code sets the compression quality when saving the image
EncoderParameters ep = new EncoderParameters ();
Long [] qy = new long [1];
Qy [0] = flag; // set the compression ratio to 1-100.
EncoderParameter eParam = new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, qy );
Ep. Param [0] = eParam;
Try
{
ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo policiciinfo = null;
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
Required iciinfo = arrayICI [x];
Break;
}
}
If (policiciinfo! = Null)
{
ISource. Save (outPath, jpegICIinfo, ep); // dFile is the new path after compression.
}
Else
{
ISource. Save (outPath, tFormat );
}
Return true;
}
Catch
{
Return false;
}
Finally
{
ISource. Dispose ();
ISource. Dispose ();
}
}
Source: http://www.cnblogs.com/lifeil/archive/2013/02/25/2931683.html