2011-05-20 100 views

回答

1

下面是我用它来降低我的图像的功能的图像完成这一点。质量很好。

private static Image ResizeImage(Image imgToResize, Size size) 
    { 
     int sourceWidth = imgToResize.Width; 
     int sourceHeight = imgToResize.Height; 

     float nPercent = 0; 
     float nPercentW = 0; 
     float nPercentH = 0; 

     nPercentW = ((float)size.Width/(float)sourceWidth); 
     nPercentH = ((float)size.Height/(float)sourceHeight); 

     if (nPercentH < nPercentW) 
      nPercent = nPercentH; 
     else 
      nPercent = nPercentW; 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap b = new Bitmap(destWidth, destHeight); 
     Graphics g = Graphics.FromImage((Image)b); 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); 
     g.Dispose(); 

     return (Image)b; 
    } 

下面是我如何使用它:

 int length = (int)stream.Length; 
     byte[] tempImage = new byte[length]; 
     stream.Read(tempImage, 0, length); 

     var image = new Bitmap(stream); 
     var resizedImage = ResizeImage(image, new Size(300, 300)); 

霍勒如果你需要帮助,得到它运行。

1

在设置InterpolationMode(MSDN链接)

你也应该看看这个链接看看:Create High Quality Thumbnail - Resize Image Dynamically

从本质上讲,你的代码看起来类似于以下内容: 位图imageToScale =新的位图(//你想减少

Bitmap bitmap = new Bitmap(imgWidth, imgHeight); 

using (Graphics graphics = Graphics.FromImage(result)) 
{ 
    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
    graphics.DrawImageimageToScale, 0, 0, result.Width, result.Height); 
    bitmap.Save(memoryStreamNew, System.Drawing.Imaging.ImageFormat.Png); 
} 

bitmap.Save(//finish this depending on if you want to save to a file location, stream, etc... 
相关问题