gpt4 book ai didi

imagemagick - 使用ImageMagick.NET和C#调整裁剪大小

转载 作者:行者123 更新时间:2023-12-04 15:02:17 25 4
gpt4 key购买 nike

我有一张大图像,想要调整为230×320(正好)。我希望系统在不损失宽高比的情况下调整其大小。也就是说,如果图片尺寸为460×650,则应先将其尺寸调整为230×325,然后再裁剪另外5个像素的高度。

我正在执行以下操作:

ImageMagickNET.Geometry geo = new ImageMagickNET.Geometry("230x320>");
img.Resize(geo);

但是图像没有被调整为230×320的确切尺寸。

我在C#4.0中使用 ImageMagick.NET

最佳答案

这就是我解决问题的方式。

private void ProcessImage(int width, int height, String filepath)
{
// FullPath is the new file's path.
ImageMagickNET.Image img = new ImageMagickNET.Image(filepath);
String file_name = System.IO.Path.GetFileName(filepath);

if (img.Height != height || img.Width != width)
{
decimal result_ratio = (decimal)height / (decimal)width;
decimal current_ratio = (decimal)img.Height / (decimal)img.Width;

Boolean preserve_width = false;
if (current_ratio > result_ratio)
{
preserve_width = true;
}
int new_width = 0;
int new_height = 0;
if (preserve_width)
{
new_width = width;
new_height = (int)Math.Round((decimal)(current_ratio * new_width));
}
else
{
new_height = height;
new_width = (int)Math.Round((decimal)(new_height / current_ratio));
}


String geomStr = width.ToString() + "x" + height.ToString();
String newGeomStr = new_width.ToString() + "x" + new_height.ToString();

ImageMagickNET.Geometry intermediate_geo = new ImageMagickNET.Geometry(newGeomStr);
ImageMagickNET.Geometry final_geo = new ImageMagickNET.Geometry(geomStr);


img.Resize(intermediate_geo);
img.Crop(final_geo);

}

img.Write(txtDestination.Text + "\\" + file_name);
}

关于imagemagick - 使用ImageMagick.NET和C#调整裁剪大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337800/

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