首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ImageMagick.NET和C#通过裁剪调整大小

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

Stack Overflow用户
提问于 2012-04-27 00:46:13
回答 1查看 6.4K关注 0票数 9

我有一个很大的图像,我想要调整到230×320 (准确地说)。我希望系统在不丢失纵横比的情况下调整它的大小。例如,如果图像是460×650,它应该首先调整到230×325,然后裁剪额外的5个像素的高度。

我正在做以下工作:

代码语言:javascript
复制
ImageMagickNET.Geometry geo = new ImageMagickNET.Geometry("230x320>");
img.Resize(geo);

但是图像的大小并没有调整到230×320的大小。

我使用的是C# 4.0中的ImageMagick.NET

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-30 23:10:19

这就是我解决这个问题的方法。

代码语言:javascript
复制
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);
    }
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10337800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档