本文介绍了C#创建缩略图(低质量和大尺寸问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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,以便您可以更好地了解问题.这是原始图片和调整大小一个.

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

有什么好的建议吗?

谢谢,
伊利(Ile)

Thanks,
Ile

推荐答案

您应该找到我对很有帮助.它包括一个使用C#进行高质量图像缩放的示例.

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

我的另一个答案中的完整示例包括如何将图像另存为jpeg.

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

这篇关于C#创建缩略图(低质量和大尺寸问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:29