在C#.net鼠标滚轮中缩放图像 [英] Zoom image in C# .net mouse wheel

查看:159
本文介绍了在C#.net鼠标滚轮中缩放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个用于加载图像的图片框,然后我想通过按ctrl+鼠标滚轮移动来放大它。 br />


如何在C#.net中执行此操作?



谢谢!

Hi,

I have a picturebox where I load an image, then I would like to zoom into it by pressing "ctrl"+ mouse wheel movements.

How can I perform this in C# .net?

Thank you!

推荐答案

这很简单。您必须将两个不相关的方面放在一起1)处理键盘和鼠标事件以及检查键盘状态,2)图形转换。你需要把它们放在一起。



首先,你需要处理事件 MouseWheel 或覆盖一个您需要响应鼠标滚轮的虚拟方法 OnMouseWheel

http://msdn.microsoft.com/en -us / library / system.windows.forms.control.mousewheel.aspx?cs-save-lang = 1& cs-lang = csharp#code-snippet-1 [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousewheel.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx [ ^ ]。



注意事件参数为您提供有关属性 Delta 中滚轮滚动值的信息。



在处理程序的代码中,您还需要检查是否按下了Ctrl。您可能还需要检查是否按下其他修饰符键,例如Alt或Shift 。您不需要检查每个密钥。而是覆盖虚拟方法 OnKeyDown OnKeyUp 或处理事件 KeyDown OnKeyUp 将响应所有关键事件。在每个键的每个事件中,修改键的状态可以改变也可以不改变。这些键的当前状态在属性值的事件参数中传递给你修饰符

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx [ ^ ],

http:// msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.modifiers.aspx [ ^ ]。



存储此值每个事件并检查鼠标滚轮事件处理程序中的最后一个修饰符状态。如果按下Ctrl(并且可能没有其他修改键),请进行缩放。



现在,实际缩放。这实际上取决于您的图形是什么以及如何渲染它。如果你试图使用 PictureBox ,它可能是位图,它不应该用放大实际放大:它会产生不可接受的质量,像素化。您只能以可接受的质量缩小尺寸,甚至放大,但只能略微缩小。如果您的实际位图数据是为最大可能尺寸准备的,那么您仍然可以这样做,因此在所有尺度上它只能减小尺寸。



但是,我不喜欢不知道,也许你的数据实际上是矢量,而你只是在一个 PictureBox 上以同样奇怪的理由呈现它(这实际上是很多初学者的常见错误)。然后你可以获得任何规模的完美图形,只有你应该摆脱 PictureBox ,即使你实际使用了一些位图。相反,您应该通过覆盖虚拟方法 OnPaint 或者直接在某些控件(包括 Form )上渲染图形,或者处理事件画出

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx [ ^ ],

http://msdn.microsoft.com/en-us/ library / system.windows.forms.control.paint.aspx [ ^ ]。



您的渲染方法应该取决于您在类字段中存储的某些数据(s),包括缩放系数。当您更改缩放(或其他任何内容)时,您可以调用其中一个 Invalidate 方法,以便重绘:

http://msdn.microsoft.com/en-us/library/598t492a.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/xz8ytzt0.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx> http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx [< a href =http://msdn.microsoft.co m / en-us / library / wtzka3b5.aspxtarget =_ blanktitle =New Window> ^ ]。



如果这是不是位图,您不必使用缩放系数重新计算所有图形。你应该更好地使用属性 System.Drawing.Graphics.Transform

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx> http://msdn.microsoft.com/en-us/library/system.drawing .graphics.transform.aspx [ ^ ]。



类的实例 System.Drawing.Graphics 将在事件参数中传递给您的绘制处理程序。



如果您想了解为什么 PictureBox 是这样的不好的目的,请看我过去的答案:

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

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

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



关于GDI +渲染和失效,请参阅我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

mdi子窗体之间的绘制线 [ ^ ]。







请参阅最新答案,提供有关缩放的更多详细信息:如何知道UserControl内部控件的鼠标位置 [ ^ ]。



-SA
This is simple enough. There are two unrelated aspects you have to put together 1) handling of keyboard and mouse events and checking keyboard status, 2) transformation of graphics. You need to put them together.

First of all, you need to handle the event MouseWheel or override a virtual method OnMouseWheel for the control you need to respond to mouse wheel:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousewheel.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousewheel.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx[^].

Pay attention that the event arguments give you the information on the value scrolled by the wheel in the property Delta.

In the code of the handler, you also need to check if Ctrl is pressed. You may also need to check if some other modifier keys, such as Alt or Shift are not pressed. You don't need to check each key though. Instead, you override the virtual methods OnKeyDown and OnKeyUp or handle the events KeyDown and OnKeyUp which will respond to all key events. In each event for each key, the state of modifier keys may or may not be changed. The current state of such keys is passed to you in event arguments in the value of the property Modifiers:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.modifiers.aspx[^].

Store this value on each event and check the last modifier state in your handler of the mouse wheel event. If Ctrl is pressed (and maybe other modifier keys are not), do the zoom.

Now, the actual zoom. It really depends on what your graphics is and how you render it. If you are trying to work with PictureBox, it could be the bitmap, which should not really be zoomed with enlargement: it would produce unacceptable quality, pixellation. You can only reduce the size with acceptable quality, or even enlarge, but only slightly. You still can do it, if your actual bitmap data is prepared for maximum possible size, so at all scales it could only be reduced in size.

However, I don't know, maybe you data is actually vector, and you just render it on a PictureBox by same weird reason (which is actually a usual big mistake of many beginners). Then you can get perfect graphics at any scale, only you should get rid of PictureBox, even if you actually use some bitmap. Instead, you should directly render your graphics on some control (including a Form), through overriding the virtual method OnPaint or handling the event Paint:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx[^].

You rendering method should depend on some data you store in your class field(s), including the zoom factor. When you change zoom (or anything else), you call one of the Invalidate methods, to cause redrawing:
http://msdn.microsoft.com/en-us/library/598t492a.aspx[^],
http://msdn.microsoft.com/en-us/library/xz8ytzt0.aspx[^],
http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx[^],
http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx[^].

If this is not a bitmap, you don't have to re-calculate all the graphics using the zoom factor. You should better use the property System.Drawing.Graphics.Transform:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^].

The instance of the class System.Drawing.Graphics will be passed to your paint handler in the event arguments.

If you want to understand why PictureBox is so bad for the purpose, please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

On GDI+ rendering and invalidation, see also my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
How to speed up my vb.net application?[^],
Drawing Lines between mdi child forms[^].



Please see the most recent answer providing more detail on zoom: How to know the mouse position for control inside UserControl[^].

—SA


这篇关于在C#.net鼠标滚轮中缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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