缩放图片图片 [英] zooming picturebox Image

查看:93
本文介绍了缩放图片图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把图像加载到图片框但是我有小图像我想图像采取图片框的大小,我知道可以通过属性拉伸来改变它但我希望通过代码缩放它

 OpenFileDialog open =  new  OpenFileDialog(); 

if (open.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
Image Background = Image.FromFile(open.FileName);
Bitmap bitmap = new 位图(open.FileName);
pictureBox1.Image =背景;

height = bitmap.Height;
width = bitmap.Width;

解决方案

解决方案很简单:不要使用图片框。此控件是冗余的,仅针对最简单的任务(显示静态图像)而设计,如果您想要更复杂的某些内容(例如缩放),则无法帮助您。它只能增加你的麻烦,浪费一些额外的资源和你的开发时间。请查看我过去的答案并建议做什么:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



图形渲染的其他一些答案:

什么样的俏皮方法是Paint DataGridView

在面板上捕获图纸

mdi子表单之间的绘制线



这是我的过去与缩放相关的答案:在Csharp网鼠标滚轮中放大图像

很抱歉,这个答案有太多的细节和重复的信息。重要的新信息是:使用转换无效。或者,您可以使用方法 System.Drawing.Graphics.DrawImage 执行图像的重新采样,该方法发生在方法所需的位置目标矩形的大小和此矩形大小与图像大小不同。请参阅:

System.Drawing.Graphics.DrawImage

您可以在我上面提到的答案中找到关于在何处使用绘图方法的说明。



现在,给你一个很大的警告:在重新采样像素图像时,只重新采样(当你以较小的尺寸渲染图像时)效果很好。充其量,您可以稍微缩放图像(更大的尺寸),否则您将获得极差的质量。该怎么做:让你原始的图像尽可能大,所以你的重新采样将大大减少渲染图像的大小。此外,关于质量,您需要利用此属性: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx

最好的渲染质量是你可以用 System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic 获得的渲染:

http://msdn.microsoft.com/en-us/library/ system.drawing.drawing2d.interpolationmode%28v = vs.110%29.aspx



-SA

Hi, I load Image to the picture box but I have small image I want image take the size of picturebox ,I Know can change it by the properties stretch but I want zooming it by code

OpenFileDialog open = new OpenFileDialog();

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Image Background = Image.FromFile(open.FileName);
                Bitmap bitmap = new Bitmap(open.FileName);
                pictureBox1.Image = Background;

                height = bitmap.Height;
                width = bitmap.Width;

解决方案

The solution is simple enough: don't use PictureBox. This control is redundant, designed only for the simplest tasks (showing static images) and won't help you if you want something a bit more complex, such as zooming. It can only add you hassles, waste some extra resources and your development time. Please see my past answers and advise what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

Some other answers on graphics rendering:
What kind of playful method is Paint DataGridView,
Capture the drawing on a panel,
Drawing Lines between mdi child forms.

And this is my past answer related to zoom: Zoom image in Csharp net mouse wheel.
Sorry that this answer has too many detail and repeated information. The essential new information is: using Transform and Invalidate. Alternatively, you can use the methods System.Drawing.Graphics.DrawImage which performs re-sampling of the image, which happens where the method expects the size of the destination rectangle and this rectangle size is different from the image size. Please see:
System.Drawing.Graphics.DrawImage.
You will find the explanation on where to use drawing methods in my answers referenced above.

Now, a big warning for you: in re-sampling of the pixel images, only re-sampling down (when you render the image in smaller size) works well. At best, you can zoom the image in (to a bigger size) only slightly, otherwise you will get extremely poor quality. What to do: make you original image as big as possible so your re-sampling would mostly reduce the rendered image size. Also, as to the quality, you will need to leverage this property: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx.
The best quality of rendering is the one you can obtain with System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx.

—SA


这篇关于缩放图片图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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