C # Implementation Image compression method

Source: Internet
Author: User
Tags diff

This is a simple implementation without optimization

 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.interpola
                Tionmode = 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 this method of compression, 200K images can only be compressed to about 160k.

Rewrite the code to achieve the following method

 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 implementation of compression makes the compression rate greatly increased. In fact, the code does not change how much, the most important is in the preservation of the time if the JPG format,

If you do not specify a format, the PNG format is used by default.

The following is written by the Friends of the garden to compress the image according to the setting of the image quality value:

 public static bool Getpicthumbnail (String sfile, string outpath, int flag) {S Ystem.
            Drawing.image ISource = System.Drawing.Image.FromFile (sfile);

            ImageFormat Tformat = Isource.rawformat;
            The following code to save the picture, set the compression quality EncoderParameters EP = new EncoderParameters ();
            long[] qy = new Long[1]; Qy[0] = flag;//Set the proportion of compression 1-100 encoderparameter Eparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Qu
            Ality, QY); Ep.
            Param[0] = Eparam;
                try {imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();
                ImageCodecInfo jpegiciinfo = null; for (int x = 0; x < Arrayici.length + +) {if (arrayici[x).
                        Formatdescription.equals ("JPEG")) {jpegiciinfo = arrayici[x];
                    Break
          }      } if (Jpegiciinfo!= null) {Isource.save (Outpath, Jpegiciinfo, EP);//dfile is the new compressed path} else {isource.save (Outpath, t
                Format);
            return true;
            catch {return false;
                finally {isource.dispose ();
            Isource.dispose (); }
        }

 

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.