This is a simple unoptimized implementation.
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:
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, the default format is png.
The following describes how to compress an image by setting the image quality value:
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 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); e P. param [0] = eParam; try {ImageCodecInfo [] arrayICI = ImageCodecInfo. getImageEncoders (); ImageCodecInfo effeciciinfo = null; for (int x = 0; x <arrayICI. length; x ++) {if (arrayICI [x]. formatDescription. equals ("JPEG") {policiciinfo = arrayICI [x]; break ;}} if (policiciinfo! = Null) {iSource. save (outPath, effeciciinfo, ep); // dFile is the new path after compression} else {iSource. save (outPath, tFormat);} return true;} catch {return false;} finally {iSource. dispose (); iSource. dispose ();}}