2009-09-07 103 views
-1

我有以下代碼來拍攝圖像並生成縮略圖。C#生成縮略圖文件大小

如何改變質量或壓縮以編程方式獲得較小的文件大小?

Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr()); 
+0

-1再次問同樣的問題:http://stackoverflow.com/questions/684092/create-thumbnail-and-reduce-image-size –

+0

究竟你是否經常要問同樣的問題? :http://stackoverflow.com/questions/672807/generating-jpg-thumbnails-in-dotnet –

回答

1

您不使用GetThumbnailImage API:

protected Stream ResizeImage(string source, int width, int height) { 
      using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(source)) 
      using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) 
      using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp)) 
      { 

       graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
       graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
       graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 

       graphic.DrawImage(bmp, 0, 0, width, height); 

       MemoryStream ms = new MemoryStream(); 
       newBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
       return ms; 

      }    
     }