C # lossless compression image,

Source: Internet
Author: User

C # lossless compression image,

/// <Summary> /// scales proportionally according to the specified size, returns true to change the size. // </summary> /// <param name = "picWidth"> image width </param> /// <param name = "picHeight"> image height </param> /// <param name = "specifiedWidth"> specify the width </param> /// <param name = "specifiedHeight"> specify the height </param> // <returns> returns true to indicate changing the size </returns> private bool GetPicZoomSize (ref int picWidth, ref int picHeight, int specifiedWidth, int specifiedHeight) {int sW = 0, sH = 0; Boolean isZoomSize = false; // proportional scaling Size tem_size = new Size (picWidth, picHeight); if (tem_size.Width> specifiedWidth | tem_size.Height> specifiedHeight) // change ** to c # or the operation symbol {if (tem_size.Width * specifiedHeight)> (tem_size.Height * specifiedWidth) {sW = specifiedWidth; sH = (specifiedWidth * tem_sizeheight) /tem_size.Width;} else {sH = specifiedHeight; sW = (tem_size.Width * speci FiedHeight)/tem_size.Height;} isZoomSize = true;} else {sW = tem_size.Width; sH = tem_size.Height;} picHeight = sH; picWidth = sW; return isZoomSize ;} /// <summary> /// lossless compression image /// </summary> /// <param name = "sFile"> original image </param> /// <param name = "dFile"> saved location after compression </param> /// <param name = "dHeight"> height </param> /// <param name = "dWidth "> width </param> /// <param name =" flag "> Compression Quality: 1-100 </param> /// <return S> </returns> public bool GetPicThumbnail (string sFile, string dFile, int dHeight, int dWidth, int flag) {System. drawing. image iSource = System. drawing. image. fromFile (sFile); ImageFormat tFormat = iSource. rawFormat; int sW = iSource. width, sH = iSource. height; GetPicZoomSize (ref sW, ref sH, dWidth, dHeight); Bitmap ob = new Bitmap (dWidth, dHeight); Graphics g = Graphics. fromImage (ob); g. clear (Colo R. 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, GraphicsUnit. pixel); g. dispose (); // The following Code sets the compression quality EncoderParameters ep = new EncoderParameters (); lon G [] 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 effeciciinfo = null; for (int x = 0; x <arrayICI. length; x ++) {if (arrayICI [x]. formatDescription. equals ("JPEG") {policiciinfo = arrayICI [x]; break ;}} If (policiciinfo! = Null) {ob. save (dFile, effeciciinfo, ep); // dFile is the new path after compression} else {ob. save (dFile, tFormat);} return true;} catch {return false;} finally {iSource. dispose (); ob. dispose ();}}

Scaling the image also changes the image size.

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.