2011-12-24 100 views
0

我有圖像共享應用程序,其中用戶上傳圖像,並採取這些圖像的縮略圖...如何,一切工作正常,但有時圖像縮略圖(600 * 800)的大小几乎是1 MB是非常巨大的有無論如何修改圖像分辨率或什麼使尺寸像..100 kb或什麼的。這是我的代碼。使圖像縮略圖更小

Bitmap bmp = new Bitmap(Width, Height); 
      System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); 
      gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
      gr.PixelOffsetMode = PixelOffsetMode.HighQuality; 

      System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, Width, Height); 
      System.Drawing.Size rs = new System.Drawing.Size(); 
      rs.Height = Height; 
      rs.Width = Width; 
      gr.DrawImage(originalImage, new Rectangle(new Point(0, 0), rs), 0, 0,  originalImage.Width, originalImage.Height, GraphicsUnit.Pixel); 
      string thumbnailPath = string.Concat(pathToSaveIn, thumbnailName); 
      bmp.Save(thumbnailPath); 
      gr.Dispose(); 
+0

這是什麼語言? Visual C++? – Blender 2011-12-24 01:52:02

回答

3

圖像大小調整代碼看起來不錯(乍一看)。但是,您將圖像保存爲位圖格式,這是無損的 - 因此文件的大小很大。

您可能想要使用JPEG代替縮略圖:對於照片等,這可以提供良好的壓縮效果。

這可能會幫助:

public void SaveImage(Bitmap image, string filename) 
    { 
     long quality = 80L; // adjust as appropriate 

     var qualityEncoder = Encoder.Quality; 

     using (var encoderParameter = new EncoderParameter(qualityEncoder, quality)) 
     using (var encoderParams = new EncoderParameters(1)) 
     { 
      encoderParams.Param[0] = encoderParameter; 
      var jpegEncoder = GetEncoder(ImageFormat.Jpeg); 
      image.Save(filename, jpegEncoder, encoderParams); 
     } 
    } 

    private static ImageCodecInfo GetEncoder(ImageFormat format) 
    { 
     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); 

     return codecs 
      .Where(codec => codec.FormatID == format.Guid) 
      .FirstOrDefault(); 
    }