如何使用C#Windows窗体调整图像大小? [英] How Can I Resize An Image Using C# Windows Forms ?

查看:325
本文介绍了如何使用C#Windows窗体调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用winforms和c#代码在下载后调整固定宽度和高度的图像。

任何解决方案都可以吗?

i want to resize an image with a fix width and heigh after downloading using winforms and c# code.
Any solution please??

推荐答案

这意味着重新采样。你需要将图像内插到不同的大小,这意味着创建一个不同的图像。



这是如何:你创建一个所需的像素格式和大小的目标位图,以像素为单位:

https://msdn.microsoft .com / zh-CN / library / 3z132tat%28v = vs.110%29.aspx [ ^ ]。



然后,你创建一个实例 System.Drawing.Graphics 可用于绘制此位图: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29。 aspx [ ^ ]。



当你使用此位图绘制,您可以在图像实例上绘制,该实例用作上面引用的 factory 函数调用的参数。现在,一个重要的时刻到来了:当您要通过插值编写新图像时,需要设置 Graphics 实例的选项以提供适当的质量。我建议几乎在所有情况下都选择最好的质量。请参阅:

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode%28v=vs.110%29.aspx [ ^ ],

https:// msdn。 microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx [ ^ ],

参见:< a href =https://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingquality%28v=vs.110%29.aspx> https://msdn.microsoft.com/en -us /库/ system.drawing.graphics.compositingquality%28V = vs.110% 29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingmode(v = vs.110)的.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.compositingquality(v = vs.110).aspx [ ^ ]。br />


完成后,在目标位图上绘制源图像: https://msdn.microsoft.com/en- us / library / system.drawing.graphics.drawimage%28v = vs.110%29.aspx [ ^ ]。



如果要填充,请使用允许您指定目标尺寸的方法之一,使其与目标图像的尺寸相同。生成的图像将通过插值拉伸/折叠到所需的大小。在大多数情况下,请确保宽高比相同。

你应该记住,重新采样位图只有在减小尺寸时才会好,在最坏的情况下,放大,但只是略微减小。任何相当大的放大都会给质量差,像素化。



另见我过去的答案:

在vb.net中调整图像大小 [ ^ ],

在流中查看Big Tiff和JPEG文件(>(23000 x 23000)pix)。并实时显示部分内容。 [ ^ ]。



-SA
This means "resampling". You need to interpolate the image to different size, which means creating a different image.

Here is how: you create a target bitmap of required pixel format and size in pixels:
https://msdn.microsoft.com/en-us/library/3z132tat%28v=vs.110%29.aspx[^].

Then, you create an instance of System.Drawing.Graphics which can be uses to draw on this bitmap: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29.aspx[^].

When you draw with this bitmap, you draw on the image instance which was used as a parameter of the call to the factory function referenced above. Now, an important moment comes: as you are going to compose a new image through interpolation, you need to setup options of the Graphics instance to provide appropriate quality. I would advise to chose the best quality in almost all cases. Please see:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx[^],
see also: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingquality%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingmode(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.compositingquality(v=vs.110).aspx[^].

When this is done, draw source image on the target bitmap: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Use one of the methods which allows you to specify the target size, make it the same as the size of target image, if you want to fill it in. The resulting image will be stretched/collapsed to the required size through interpolation. In most cases, make sure the aspect ratio is the same.
You should remember that resampling bitmaps can only be good if you reduce the size, in worst case, enlarge, but only slightly. Any considerable enlargement will give bad quality, pixellation.

See also my past answers:
resize image in vb.net[^],
Read Big Tiff and JPEG files (>(23000 x 23000) pix) in a stream. And display part of it to the screen in realtime.[^].

—SA


这篇关于如何使用C#Windows窗体调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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