为了账号安全,请及时绑定邮箱和手机立即绑定

在asp.net中调整图像大小而不丢失图像质量

在asp.net中调整图像大小而不丢失图像质量

弑天下 2019-12-26 14:21:26
我正在开发一个ASP.NET 3.5 Web应用程序,该应用程序允许我的用户上传jpeg,gif,bmp或png图像。如果上载的图像尺寸大于103 x 32,则我希望将上载的图像尺寸调整为103 x32。我已经阅读了一些博客文章和文章,还尝试了一些代码示例,但似乎没有任何用处。有没有人成功做到这一点?
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

不久前,我遇到了同样的问题,并以这种方式处理:


private Image RezizeImage(Image img, int maxWidth, int maxHeight)

{

    if(img.Height < maxHeight && img.Width < maxWidth) return img;

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

        Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);

        using (Graphics gr = Graphics.FromImage(cpy))

        {

            gr.Clear(Color.Transparent);


            // This is said to give best quality when resizing images

            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;


            gr.DrawImage(img,

                new Rectangle(0, 0, nnx, nny),

                new Rectangle(0, 0, img.Width, img.Height),

                GraphicsUnit.Pixel);

        }

        return cpy;

    }


}


private MemoryStream BytearrayToStream(byte[] arr)

{

    return new MemoryStream(arr, 0, arr.Length);

}


private void HandleImageUpload(byte[] binaryImage)

{

    Image img = RezizeImage(Image.FromStream(BytearrayToStream(binaryImage)), 103, 32);

    img.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif);

}

我刚刚读到,这是获得最高质量的方法。


查看完整回答
反对 回复 2019-12-26
  • 3 回答
  • 0 关注
  • 658 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信