使用ImageMagick.NET和C通过裁剪调整大小#

使用ImageMagick.NET和C通过裁剪调整大小#,imagemagick,imagemagick.net,Imagemagick,Imagemagick.net,我有一个大图像,我想调整到230×320(准确)。我希望系统在不丢失纵横比的情况下调整其大小。i、 e.如果图像为460×650,则应首先将其大小调整为230×325,然后裁剪额外的5像素高度 我正在做以下工作: ImageMagickNET.Geometry geo = new ImageMagickNET.Geometry("230x320>"); img.Resize(geo); 但是图像的大小没有调整到230×320的准确大小 我在C#4.0中使用。这就是我解决问题的方法 pri

我有一个大图像,我想调整到230×320(准确)。我希望系统在不丢失纵横比的情况下调整其大小。i、 e.如果图像为460×650,则应首先将其大小调整为230×325,然后裁剪额外的5像素高度

我正在做以下工作:

ImageMagickNET.Geometry geo = new ImageMagickNET.Geometry("230x320>");
img.Resize(geo);
但是图像的大小没有调整到230×320的准确大小


我在C#4.0中使用。

这就是我解决问题的方法

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

谢谢你,托尼!我一直在寻找这个问题的解决方案,所有答案都与命令行实用程序有关。很高兴知道其他人也在使用.NETAPI