C# 创建缩略图(低质量和大尺寸问题)

发布于 2024-09-01 19:53:04 字数 1059 浏览 3 评论 0原文

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 以便您可以更好地了解问题。 这是原始图像和调整大小 1。

有什么好的推荐吗?

谢谢,

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();
        }

I've put 700px so that you can have better insight of the problem.
Here is original image and resized one.

Any good recommendation?

Thanks,
Ile

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

香草可樂 2024-09-08 19:53:04

您应该会发现我对这个问题的回答很有帮助。它包括一个用 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; 
    } 

You should find my answer to this question helpful. It includes a sample for high quality image scaling in C#.

The full sample in my other answer includes how to save the image as a jpeg.

Here's the relevant bit of code...

    /// <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; 
    } 
枕头说它不想醒 2024-09-08 19:53:04

如果图像包含缩略图,它会自动将其拉伸到所需的大小...这会让它看起来像垃圾(就像你的情况;))

直接从 MSDN 中...

如果图像包含嵌入的
缩略图,此方法检索
嵌入的缩略图并对其进行缩放
到要求的尺寸。如果图像
不包含嵌入的缩略图
图像,此方法创建缩略图
通过缩放主图像来生成图像。

在你的情况下,我只是仔细检查了源图像的缩略图并得到了这个......

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

If the image contains a thumbnail it'll automatically stretch it to the desired size...which will make it look like crap (like your case ;))

Straight outta MSDN...

If the Image contains an embedded
thumbnail image, this method retrieves
the embedded thumbnail and scales it
to the requested size. If the Image
does not contain an embedded thumbnail
image, this method creates a thumbnail
image by scaling the main image.

In your case I just double checked the source image for it's thumbnail and got this...

  • New Windows Thumbnail : JPEG Format (Offset:830Size:3234)
  • Thumbnail Type : JPEG
  • Thumnail Width : 112
  • Thumbnail Height : 84
往事风中埋 2024-09-08 19:53:04

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

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);

Try to draw the original image to another smaller image, and save the result.

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);
别闹i 2024-09-08 19:53:04

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

http://imagemagick.codeplex.com/

Are you allowed to use third party applications? If so, you may want to check out ImageMagick to manage thumbnail creation. There's a .NET wrapper.

http://imagemagick.codeplex.com/

御守 2024-09-08 19:53:04

我编写了一个免费的 .dll 可以轻松完成此操作。如果您想查看文档,就在这里......
Git 存储库和教程

I wrote a free .dll that does this easily. It's here if you want to see the documentation....
Git Repository & Tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文