熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#實現圖片壓縮方法

2022-06-13   來源: .NET編程 

  這個是未經優化的簡單實現

  public static SystemDrawingImage GetImageThumb(SystemDrawingImage sourceImg int width int height) { SystemDrawingImage targetImg = new SystemDrawingBitmap(width height); using (SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(targetImg)) { gInterpolationMode = SystemDrawingDrawingDInterpolationModeHigh; gSmoothingMode = SystemDrawingDrawingDSmoothingModeHighQuality; gInterpolationMode = SystemDrawingDrawingDInterpolationModeHighQualityBicubic; gCompositingQuality = SystemDrawingDrawingDCompositingQualityHighQuality; gPixelOffsetMode = SystemDrawingDrawingDPixelOffsetModeHighQuality; gDrawImage(sourceImg new SystemDrawingRectangle( width height) new SystemDrawing Rectangle( sourceImgWidth sourceImgHeight) SystemDrawingGraphicsUnitPixel); gDispose(); } return targetImg; }

  這個方法比較簡單用到的是高質量壓縮經過這個方法壓縮後K的圖片只能壓縮到k左右

  經過改寫代碼實現了如下的方法

  public Bitmap GetImageThumb(Bitmap mg Size newSize) { double ratio = d; double myThumbWidth = d; double myThumbHeight = d; int x = ; int y = ; Bitmap bp; if ((mgWidth / ConvertToDouble(newSizeWidth)) > (mgHeight / ConvertToDouble(newSizeHeight))) ratio = ConvertToDouble(mgWidth) / ConvertToDouble(newSizeWidth); else ratio = ConvertToDouble(mgHeight) / ConvertToDouble(newSizeHeight); myThumbHeight = MathCeiling(mgHeight / ratio); myThumbWidth = MathCeiling(mgWidth / ratio); Size thumbSize = new Size((int)newSizeWidth (int)newSizeHeight); bp = new Bitmap(newSizeWidth newSizeHeight); x = (newSizeWidth thumbSizeWidth) / ; y = (newSizeHeight thumbSizeHeight); SystemDrawingGraphics g = GraphicsFromImage(bp); gSmoothingMode = SmoothingModeHighQuality; gInterpolationMode = InterpolationModeHighQualityBicubic; gPixelOffsetMode = PixelOffsetModeHighQuality; Rectangle rect = new Rectangle(x y thumbSizeWidth thumbSizeHeight); gDrawImage(mg rect mgWidth mgHeight GraphicsUnitPixel); return bp; }

  這樣實現的壓縮使壓縮率大幅度上升其實代碼並沒有變多少最主要的是在保存的時候要是用jpg格式

  如果不指定格式默認使用的是png格式

  下面這個是園友寫的根據設置圖片質量數值來壓縮圖片的方法

  public static bool GetPicThumbnail(string sFile string outPath int flag) { SystemDrawingImage iSource = SystemDrawingImageFromFile(sFile); ImageFormat tFormat = iSourceRawFormat; //以下代碼為保存圖片時設置壓縮質量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[]; qy[] = flag;//設置壓縮的比例 EncoderParameter eParam = new EncoderParameter(SystemDrawingImagingEncoderQuality qy); epParam[] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfoGetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = ; x < arrayICILength; x++) { if (arrayICI[x]FormatDescriptionEquals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { iSourceSave(outPath jpegICIinfo ep);//dFile是壓縮後的新路徑 } else { iSourceSave(outPath tFormat); } return true; } catch { return false; } finally { iSourceDispose(); iSourceDispose(); } }


From:http://tw.wingwit.com/Article/program/net/201311/14025.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.