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

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

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441059.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存