gpt4 book ai didi

c# - 将图像调整为特定宽度并固定高度

转载 作者:行者123 更新时间:2023-11-30 17:58:40 25 4
gpt4 key购买 nike

我想调整大图像的宽度为 150 像素,高度将固定。然后将图像保存到我网站的文件夹中。

例子:(宽度,高度)如果我将图像调整为 300px*300px,我将得到调整后的图像为 150px*150px。如果我将图像调整为 450px*300px,我将得到调整后的图像为 150px*100px。

重点是宽度和高度之间的比例将始终保持 150 像素的宽度。

有什么帮助吗?

最佳答案

我找到了这个(不是我的)here

private Bitmap ScaleImage(Image oldImage)
{
double resizeFactor = 1;

if (oldImage.Width > 150 || oldImage.Height > 150)
{
double widthFactor = Convert.ToDouble(oldImage.Width) / 150;
double heightFactor = Convert.ToDouble(oldImage.Height) / 150;
resizeFactor = Math.Max(widthFactor, heightFactor);

}
int width = Convert.ToInt32(oldImage.Width / resizeFactor);
int height = Convert.ToInt32(oldImage.Height / resizeFactor);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}

关于c# - 将图像调整为特定宽度并固定高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com