113

如何在不影响图像质量的情况下调整图像大小?

4

11 回答 11

221

正如rcar所说,你不能不损失一些质量,你可以在 c# 中做的最好的事情是:

Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
    gr.SmoothingMode = SmoothingMode.HighQuality;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
于 2008-09-17T21:20:27.360 回答
32

除非您正在制作矢量图形,否则无法在不损失一些图像质量的情况下调整图像大小。

于 2008-09-17T21:17:17.057 回答
26
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;
}

这里

于 2012-05-24T09:54:21.257 回答
5

我相信您要做的是“调整大小/重新采样”您的图像。这是一个提供说明并提供实用程序类的好网站(我也碰巧使用):

http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx

于 2009-01-06T10:50:14.043 回答
3

您无法在不损失一些质量的情况下调整图像大小,这仅仅是因为您正在减少像素数量。

不要减小客户端的大小,因为浏览器在调整图像大小方面做得不好。

您可以做的是在渲染之前或在用户上传之前以编程方式更改大小。

这是一篇文章,解释了在 c# 中执行此操作的一种方法:http: //www.codeproject.com/KB/GDI-plus/imageresize.aspx

于 2008-09-17T21:23:59.897 回答
3

除非您调整大小,否则您无法对光栅图形执行此操作。

您可以通过良好的过滤和平滑来调整大小而不会失去任何明显的质量。

您还可以更改图像的 DPI 元数据(假设它有一些),这将保持完全相同的像素数,但会改变图像编辑器在“真实世界”测量中对它的看法。

只是为了涵盖所有基础,如果您真的只是指图像的文件大小而不是实际图像尺寸,我建议您查看图像数据的无损编码。我对此的建议是将图像重新保存为 .png 文件(我倾向于使用绘画作为 Windows 中图像的免费转码器。在绘画中加载图像,另存为新格式)

于 2008-09-17T21:26:17.783 回答
2

看看你是否喜欢这个开源 ASP.NET 模块的图像大小调整质量。有一个现场演示,所以你可以自己弄乱它。它产生的结果(对我而言)无法与 Photoshop 输出区分开来。它也有类似的文件大小——MS 在他们的 JPEG 编码器上做得很好。

于 2008-11-05T21:13:51.743 回答
2

在这里您还可以找到在此类中添加水印代码:

public class ImageProcessor
    {
        public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
        {
            try
            {
                Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));

                using (Graphics gr = Graphics.FromImage(newImage))
                {
                    gr.SmoothingMode = SmoothingMode.AntiAlias;
                    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));

                    var myBrush = new SolidBrush(Color.FromArgb(70, 205, 205, 205));

                    double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);

                    Rectangle containerBox = new Rectangle();

                    containerBox.X = (int)(diagonal / 10);
                    float messageLength = (float)(diagonal / message.Length * 1);
                    containerBox.Y = -(int)(messageLength / 1.6);

                    Font stringFont = new Font("verdana", messageLength);

                    StringFormat sf = new StringFormat();

                    float slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);

                    gr.RotateTransform(slope);
                    gr.DrawString(message, stringFont, myBrush, containerBox, sf);
                    return newImage;
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public int Calculations(decimal w1, decimal h1, int newWidth)
        {
            decimal height = 0;
            decimal ratio = 0;


            if (newWidth < w1)
            {
                ratio = w1 / newWidth;
                height = h1 / ratio;

                return height.To<int>();
            }

            if (w1 < newWidth)
            {
                ratio = newWidth / w1;
                height = h1 * ratio;
                return height.To<int>();
            }

            return height.To<int>();
        }

    }
于 2013-02-05T16:02:09.027 回答
1

有一些东西,上下文感知调整大小,不知道你是否能够使用它,但值得一看,这是肯定的

一个不错的视频演示(放大出现在中间) http://www.youtube.com/watch?v=vIFCV2spKtg

这里可能有一些代码。 http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/

那是矫枉过正吗?也许您可以将一些简单的过滤器应用于放大的图像以稍微模糊像素,您可以研究一下。

于 2008-09-17T21:30:01.347 回答
1

你是调整大还是小?是小百分比还是更大的因素,例如 2 倍、3 倍?您的应用程序的质量是什么意思?什么类型的图像——照片、硬边线条图,还是什么?编写自己的低级像素研磨代码或尝试尽可能多地使用现有库(.net 或其他)?

关于这个主题有大量的知识。关键概念是插值。

浏览建议:
* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
* http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
* 对于 C#:https: //secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945 *这是特定于java的,但可能具有教育意义-http: //today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

于 2008-09-17T21:36:47.773 回答
0

这是一个提供 C# 图像大小调整代码示例的论坛主题。您可以使用GD 库绑定器之一在 C# 中进行重新采样。

于 2008-09-17T21:42:08.323 回答