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.