C # Image Compression Method

Source: Internet
Author: User

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

 

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.