2010-05-18 106 views
3
public void CreateThumbnail(Image img1, Photo photo, string targetDirectoryThumbs) 
     { 
      int newWidth = 700; 
      int newHeight = 700; 
      double ratio = 0; 

      if (img1.Width > img1.Height) 
      { 
       ratio = img1.Width/(double)img1.Height; 
       newHeight = (int)(newHeight/ratio); 
      } 
      else 
      { 
       ratio = img1.Height/(double)img1.Width; 
       newWidth = (int)(newWidth/ratio); 
      } 

      Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); 
      bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg"); 

      img1.Dispose(); 
      bmp1.Dispose(); 
     } 

我已经放了700px以便您可以更好地了解问题。 这里是original图片和resized之一。C#创建缩略图(低质量和大尺寸问题)

有什么好的建议吗?

感谢,

+1

虽然扩展名是“jpg”,你实际上是保存在PNG格式(默认)的BMP1图像。要让Jpeg使用bmp1.Save(“name”,ImageFormat.Jpeg)。 – munissor 2010-05-18 13:56:45

回答

5

你会发现我的回答this question很有帮助。它包含一个用于在C#中进行高质量图像缩放的示例。

我的其他答案的完整示例包括如何将图像保存为jpeg。

这里是代码的相关位...

/// <summary> 
    /// Resize the image to the specified width and height. 
    /// </summary> 
    /// <param name="image">The image to resize.</param> 
    /// <param name="width">The width to resize to.</param> 
    /// <param name="height">The height to resize to.</param> 
    /// <returns>The resized image.</returns> 
    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
    { 
     //a holder for the result 
     Bitmap result = new Bitmap(width, height); 

     //use a graphics object to draw the resized image into the bitmap 
     using (Graphics graphics = Graphics.FromImage(result)) 
     { 
      //set the resize quality modes to high quality 
      graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      //draw the image into the target bitmap 
      graphics.DrawImage(image, 0, 0, result.Width, result.Height); 
     } 

     //return the resulting bitmap 
     return result; 
    } 
+0

Ile ... Docta答案的重要部分之一是: 图形。InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 在您的原始代码中,每个像素都会移动并变大,而不考虑相邻像素。插值,平滑和合成是关键。 – Rusty 2010-05-18 14:06:16

+0

如何将缩略图保存为文件?我试过result.Save(targetDirectoryThumbs + photo.PhotoID +“.jpg”);但我得到空白图像?谢谢! – 2010-05-18 14:33:49

+0

ile,看看我链接到的答案,有一个名为SaveJpeg的函数,您会发现它很有用。 – 2010-05-18 14:35:33

1

尝试将原始图像绘制到另一个更小的图像,并保存结果。

Bitmap bmp1 = new Bitmap(newWidth, newHeight); 
Graphics g = Graphics.FromImage(bmp); 
g.DrawImage(img1, 0, 0, newWidth, newHeight); 
bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg", ImageFormat.Jpeg); 
+0

这工作很好......谢谢munissor :) – 2010-05-18 14:49:43

0

您允许使用第三方应用程序吗?如果是这样,您可能需要检出ImageMagick来管理缩略图创建。有一个.NET包装。

http://imagemagick.codeplex.com/

+0

我被允许,但我认为内置的解决方案应该做的工作(我没有看到很多抱怨:))。感谢您的指教! – 2010-05-18 14:49:04

2

如果图像包含的缩略图,它会自动将其拉伸至所需尺寸...这将使它看起来像废话(像你的情况;))

直失控MSDN ...

如果图像包含嵌入 缩略图图像时,该方法检索 嵌入缩略图和刻度s它 到请求的大小。如果图像 不包含嵌入的缩略图 图像,则此方法通过缩放主图像来创建缩略图 图像。

在你的情况我只是双重检查源图像为它的缩略图,并得到这个...

  • 新的Windows缩略图:JPEG格式(偏移:830Size:3234)
  • 缩略类型: JPEG
  • 略图宽度:112
  • 缩略图高度:84
+0

感谢您提供的信息,我总是想知道为什么会出现这种情况! – 2010-05-21 10:41:15

0

我瓦特狂妄自由的.dll,这很容易做到这一点。它在这里,如果你想看到文档.... Git Repository & Tutorial