如何在.NET中一步裁剪和调整图像大小 [英] How to crop and resize image in one step in .NET

查看:97
本文介绍了如何在.NET中一步裁剪和调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Image文件,我想使用System.Drawing类同时裁剪和调整大小

I have an Image file that I would like to crop and resize at the same time using the System.Drawing class

我正在尝试基于本文中找到的思想: http://www.schnieds.com/2011/07/image-upload-crop-and-resize-with.html

I am trying to build upon the ideas found in this article :http://www.schnieds.com/2011/07/image-upload-crop-and-resize-with.html

我能够单独裁剪和调整大小,但是当我尝试组合该过程时,会得到一些奇怪的输出.

I am able to Crop and Resize seperately but when I try to combine the process, I am getting some strange output.

这是我一直在尝试的

using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(w, h))
{
    _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    using (Graphics _graphic = Graphics.FromImage(_bitmap))
    {
        _graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        _graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        _graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

        //Code used to crop
        _graphic.DrawImage(img, 0, 0, w, h);
        _graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);

        //Code I used to resize
        _graphic.DrawImage(img, 0, 0, img.Width, img.Height);
        _graphic.DrawImage(img, new Rectangle(0, 0, W_FixedSize, H_FixedSize), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);



       //continued...
    }
}

在上面的代码中,注释了两部分...一个区域进行裁剪,一个区域进行大小调整.

In the above code...there are two sections commented...one to crop and one one to resize.

对于裁剪,我将图像的适当坐标和宽度/高度部分传递给裁剪(x,y,w,h).

For cropping, I pass in the proper coords and width/height part of the image to crop(x, y, w, h).

我想根据自己的参数进行裁剪,并根据W_FixedSize和H_Fixed size参数绘制图像.

I would like to crop based on my parameters and draw the image based on the W_FixedSize and H_Fixed size params.

推荐答案

由于GDI中的错误,错过了所有答案的一件事是,生成的图像在图像周围将具有50%的透明1像素边框.

One thing all of the answers missed is that the resulting image will have a 50% transparent 1 pixel border around the image, due to a bug in GDI.

要正确裁剪和调整大小,您需要将以下设置应用于图形对象:

To properly crop and resize, you need to apply the following settings to the graphics object:

        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;

然后,您需要创建一个ImageAttributes实例来解决边界错误:

Then you need to make an ImageAttributes instance to fix the border bug:

ImageAttributes ia = new ImageAttributes();
ia.SetWrapMode(WrapMode.TileFlipXY);

然后,在调用DrawImage时,将ia作为最后一个参数传递.

Then, when calling DrawImage, pass ia as the last parameter.

如果要处理任何PNG,TIFF或ICO图像并将其转换为不支持透明的格式,则还需要在调用DrawImage之前调用g.Clear(bgcolor).

If you're dealing with any PNG, TIFF, or ICO images and converting them to a format that doesn't support transparency, you also need to call g.Clear(bgcolor) prior to calling DrawImage.

如果您要编码为jpeg格式,请确保设置Quality参数,然后再处置EncoderParameters对象.

If you're encoding to jpeg format, make sure to set the Quality parameter and dispose of the EncoderParameters object afterwards.

您正在读取的Bitmap实例将锁定基础文件,直到将其释放为止.如果使用FromStream方法,则必须保持流打开,直到释放Bitmap实例为止.一个很好的方法是将流克隆到MemoryStream实例中,并将其分配给Bitmap.Tag属性.

The Bitmap instance that you are reading from will lock the underlying file until after it is disposed. If you use the FromStream method, you must keep the stream open until after the Bitmap instance is disposed. A good way to do this is clone the stream into a MemoryStream instance and assign it to the Bitmap.Tag property.

我有一个 GDI +裁剪&的完整列表.调整错误大小以免在我的博客上出现.

我通常会尝试将人们推送到使用我的imageresizing.net库,因为该库旨在安全地在网站上运行具有最佳性能. 1行代码,几乎没有用户错误的空间.

I usually try to push people to use my imageresizing.net library, as it's designed to operate safely on a website with optimum performance. 1 line of code, and very little room for user error.

我下载了Schnieds的示例项目,我不得不说这是一种(不必要的)复杂的工作方式.实际上,非破坏性编辑要容易得多,文章.可以轻松地将它与Uploadify结合使用,尽管我没有在博客中介绍.

I downloaded Schnieds' example project, and I have to say it's an (unnecessarily) complicated way of doing things. Non-destructive editing is actually much easier, as shown on this article. It's easy to combine with Uploadify, although I don't cover that on the blog.

此外,对于jpeg和png文件,在上传过程中对图像进行重新编码也具有破坏性.验证是好的,但是只需在验证之后处理该实例,不要对其进行重新编码. Schnieds的示例还通过未处理的Bitmap实例泄漏了内存-在大容量服务器上运行它会迅速使其崩溃.

Also, re-encoding the image during upload is very destructive, both for jpeg and png files. Validation is good, but just dispose the instance after validation, don't re-encode it. Schnieds' example also leaks memory through the undisposed Bitmap instance - running it on a high-volume server would crash it quickly.

这篇关于如何在.NET中一步裁剪和调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆