栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号