2012-06-22 43 views
1

我正在为摄影师创建一个投资组合网站。如何处理大图像大小?

我面临将照片大小调整为小的问题。如果我缩小了尺寸,许多质量都会丢失。我如何压缩图片以免失去质量?

我的代码:

using (var input = new Bitmap(imageFile.InputStream)) 
      { 
       int width; 
       int height; 
       if (input.Width > input.Height) 
       { 
        width = 411 * input.Width/input.Height; 
        height = 411; 
       } 
       else 
       { 
        height = 411; 
        width = 411 * input.Width/input.Height; 
       } 

       using (var thumb = new Bitmap(width, height)) 
       using (var graphic = Graphics.FromImage(thumb)) 
       { 
        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 
        graphic.SmoothingMode = SmoothingMode.AntiAlias; 
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 

        graphic.DrawImage(input, 0, 0, width, height); 
        using (var output = System.IO.File.Create(imagePath)) 
        { 
         thumb.Save(output, ImageFormat.Jpeg); 
        } 
       } 
      } 
+0

你想放大图片吗? – Botonomous

+1

[高质量图像缩放C#]的可能重复(http://stackoverflow.com/questions/249587/high-quality-image-scaling-c-sharp) –

回答

2

可能取得更好的成绩做逐步调整大小(如10-25%的每一步)。尝试保存在无损格式(如ImageFormat.Png)。

如果质量是非常重要的手动转换在适当的照片编辑工具可能是正确的做法。