我有一个很大的图像,我想要调整到230×320 (准确地说)。我希望系统在不丢失纵横比的情况下调整它的大小。例如,如果图像是460×650,它应该首先调整到230×325,然后裁剪额外的5个像素的高度。
我正在做以下工作:
ImageMagickNET.Geometry geo = new ImageMagickNET.Geometry("230x320>");
img.Resize(geo);但是图像的大小并没有调整到230×320的大小。
我使用的是C# 4.0中的ImageMagick.NET。
发布于 2012-04-30 23:10:19
这就是我解决这个问题的方法。
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);
}https://stackoverflow.com/questions/10337800
复制相似问题