0

可能重复:
在不损失任何质量的情况下调整图像大小

在用户从 FileUpload 上传图像并保存在服务器上以减小大小之后,我总是在我的应用程序中使用的代码很好。但是,当我在 Photoshop(手动)上做同样的事情并在 facebook 上上传文件时,质量会更好,而且减小的尺寸非常公平。

我只是不知道如何提高服务器上上传图像的质量,有人使用不同的方法吗?

见下面的代码:

        private static byte[] ReturnReducedImage(byte[] original, int width, int height)
    {
        Image img = RezizeImage(Image.FromStream(BytearrayToStream(original)), width, height);
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }


    private static Image RezizeImage(Image img, int maxWidth, int maxHeight)
    {
        if (img.Height < maxHeight && img.Width < maxWidth) return img;
        Bitmap cpy = null;
        using (img)
        {
            Double xRatio = (double)img.Width / maxWidth;
            Double yRatio = (double)img.Height / maxHeight;
            Double ratio = Math.Max(xRatio, yRatio);
            int nnx = (int)Math.Floor(img.Width / ratio);
            int nny = (int)Math.Floor(img.Height / ratio);
            cpy = new Bitmap(nnx, nny);
            using (Graphics gr = Graphics.FromImage(cpy))
            {
                gr.Clear(Color.Transparent);
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gr.DrawImage(img, new Rectangle(0, 0, nnx, nny), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
            }
        }
        return cpy;
    }
4

1 回答 1

2

I believe this answers the question: https://stackoverflow.com/a/87786/910348.

You'll want to replace this:

using (Graphics gr = Graphics.FromImage(cpy))
{
    gr.Clear(Color.Transparent);
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    gr.DrawImage(img, new Rectangle(0, 0, nnx, nny), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}

with something like this:

using (Graphics gr = Graphics.FromImage(cpy))
{
    gr.Clear(Color.Transparent);
    gr.SmoothingMode = SmoothingMode.AntiAlias;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(img, new Rectangle(0, 0, nnx, nny));
}

Cheers.

于 2012-05-13T23:17:34.120 回答