减小图像尺寸,但保持最高质量/分辨率?

发布于 2024-11-08 20:25:34 字数 120 浏览 0 评论 0原文

我正在使用 bitmap.GetThumbnailImage() 调整图像大小,但是我似乎失去了图像质量/分辨率。例如,原始图像的分辨率为 300dpi,调整后的 1 要小得多。

调整大小时如何保持图像分辨率?

I'm resizing an image using bitmap.GetThumbnailImage() however I seem to be losing image quality/resolution. For example the original image was 300dpi resolution, and the resized 1 is much smaller.

How can I retain the image resolution when resizing?

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

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

发布评论

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

评论(2

最美不过初阳 2024-11-15 20:25:34

这是我用来缩小图像的函数。质量非常好。

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

如果您需要帮助来运行它,请大声喊叫。

Here's a function I use to reduce my images. The quality is excellent.

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

Here's how I use it:

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

Holler if you need help getting it running.

≈。彩虹 2024-11-15 20:25:34

看看设置 InterpolationMode (MSDN 链接)

您还应该采取看一下这个链接:创建高品质缩略图 - 动态调整图像大小

本质上,您的代码类似于以下内容:
Bitmap imageToScale = new bitmap( //用要缩小的图像完成此操作

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...

Take a look at setting the InterpolationMode (MSDN link)

You should also take a look at this link: Create High Quality Thumbnail - Resize Image Dynamically

Essentially, you have code that looks similar to the following:
Bitmap imageToScale = new bitmap( //finish this with the image you want to reduce

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