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

c#在保留宽高比的同时将图像调整为不同大小

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

c#在保留宽高比的同时将图像调整为不同大小

我从此CodeProject文章中学到了如何调整图像大小和填充图像。

static Image FixedSize(Image imgPhoto, int Width, int Height)    {        int sourceWidth = imgPhoto.Width;        int sourceHeight = imgPhoto.Height;        int sourceX = 0;        int sourceY = 0;        int destX = 0;        int destY = 0;        float nPercent = 0;        float nPercentW = 0;        float nPercentH = 0;        nPercentW = ((float)Width / (float)sourceWidth);        nPercentH = ((float)Height / (float)sourceHeight);        if (nPercentH < nPercentW)        { nPercent = nPercentH; destX = System.Convert.ToInt16((Width -    (sourceWidth * nPercent)) / 2);        }        else        { nPercent = nPercentW; destY = System.Convert.ToInt16((Height -    (sourceHeight * nPercent)) / 2);        }        int destWidth = (int)(sourceWidth * nPercent);        int destHeight = (int)(sourceHeight * nPercent);        Bitmap bmPhoto = new Bitmap(Width, Height,    PixelFormat.Format24bppRgb);        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,   imgPhoto.VerticalResolution);        Graphics grPhoto = Graphics.FromImage(bmPhoto);        grPhoto.Clear(Color.Red);        grPhoto.InterpolationMode =     InterpolationMode.HighQualityBicubic;        grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);        grPhoto.Dispose();        return bmPhoto;    }


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

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

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