C # Lossless high-quality compressed pictures

Source: Internet
Author: User

These days in the same city dating network www.niyuewo.com encountered a problem, how to compress the member's avatar, search on the Internet to organize as follows: In this also thanks to the medical essence (www.yiyaojing.com) Webmaster's help

 <summary>//lossless compression picture///</summary>//<param name= "SFile" > Original picture </para m>//<param name= "Dfile" > Save location after compression </param>//<param name= "dheight" > Height &LT;/PARAM&G          T          <param name= "Dwidth" ></param>//<param name= "flag" > Compression quality (the smaller the number, the higher the compression rate) 1-100</param> <returns></returns> public static bool Getpicthumbnail (string sFile, string dfile, int dHe                        ight, int dwidth, int flag) {System.Drawing.Image ISource = System.Drawing.Image.FromFile (sFile);            ImageFormat Tformat = Isource.rawformat;            int SW = 0, SH = 0;            Scale Size tem_size = new Size (isource.width, isource.height); if (tem_size. Width > Dheight | | Tem_size. Width > Dwidth)//Change the * * to C # or manipulate the symbol {if (tem_size. Width * dheight) > (tem_size.              Height * dwidth))  {SW = Dwidth; SH = (Dwidth * tem_size. Height)/tem_size.                Width;                    } else {SH = Dheight; SW = (tem_size. Width * dheight)/tem_size.                Height; }} else {SW = tem_size.                Width; SH = Tem_size.            Height;            } Bitmap ob = new Bitmap (dwidth, dheight);            Graphics g = graphics.fromimage (OB);            G.clear (Color.whitesmoke);            g.compositingquality = compositingquality.highquality;            G.smoothingmode = smoothingmode.highquality;            G.interpolationmode = Interpolationmode.highqualitybicubic; G.drawimage (ISource, New Rectangle ((DWIDTH-SW)/2, (DHEIGHT-SH)/2, SW, SH), 0, 0, isource.width, Isource.height, Gr            Aphicsunit.pixel);            G.dispose (); The following code sets the compression quality EncoderParameters EP = new Encoderparamete when saving a pictureRS ();            long[] qy = new Long[1]; Qy[0] = flag;//Set the scale of the compression 1-100 encoderparameter Eparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Qua            Lity, QY); Ep.            Param[0] = Eparam;                try {imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();                ImageCodecInfo jpegiciinfo = null; for (int x = 0, x < arrayici.length, x + +) {if (arrayici[x].                        Formatdescription.equals ("JPEG")) {jpegiciinfo = arrayici[x];                    Break                }} isource.dispose (); if (jpegiciinfo! = null) {ob. Save (Dfile, Jpegiciinfo, EP);//dfile is the new path after compression} else {OB .                Save (Dfile, Tformat);            } return true;        } catch    {return false; } finally {ob.            Dispose ();   }        }

  

C # Lossless high-quality compressed pictures

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.