如何在 vb.net 中使用滚轮放大图片框 [英] How to zoom in a Picturebox with scrollwheel in vb.net
问题描述
我正在使用一组图形叠加层在使用图形对象的图片框控件内绘制图像.我已将图片框放置在面板内并将面板设置为自动滚动.我现在需要知道如何做的是使用鼠标滚轮以小增量放大图片的大小,同时保持所绘制图像的质量.有人知道怎么做吗?
I'm using a set of graphics overlays to draw an image inside a picturebox control using the graphics object. I have placed the Picturebox inside a Panel and set the Panel to autoscroll. What I need to know how to do now is use the Mouse scroll wheel to blow up the size of the picture in small increments while maintaining the quality of the image drawn. Anyone know how to do this?
当我使用下面的 Abdias 软件代码进行更新时,当图片框的 Sizemode 属性设置为 StretchImage 时,图片开始变小.我有一个鼠标平移功能,可能会干扰此代码的正常工作.有任何想法吗?是什么导致它无法正常工作?
When I update with Abdias Software code below, the picture starts out smaller when Sizemode property of picturebox is set to StretchImage. I have a pan feature with the mouse that might be interfering with keeping this code from working properly. Any Ideas? What could be keeping this from working properly?
已解决
这段代码对我来说比下面两个代码都好得多:
This code worked much better for me than any of the two below:
Private Sub PictureBox_MouseWheel(sender As System.Object,
e As MouseEventArgs) Handles PictureBox1.MouseWheel
If e.Delta <> 0 Then
If e.Delta <= 0 Then
If PictureBox1.Width < 500 Then Exit Sub 'minimum 500?
Else
If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000?
End If
PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
End If
End Sub
推荐答案
你可以试试这个代码.它假设表单上存在 Panel1
和 PictureBox1
(PictureBox1
在 Panel1
中,Panel1.AutoScroll = True
) 并在 PictureBox
上设置图像.
You can try this code. It assumes there exist a Panel1
and PictureBox1
on the form (PictureBox1
inside the Panel1
with Panel1.AutoScroll = True
) with an image set on the PictureBox
.
代码不计算缩放的中心点,但您可以使用 e.Location(或 e.X/e.Y).
The code doesn't calculate center point of the zoom, but you can use the e.Location (or e.X/e.Y) for that.
更新 - 这是(应该)比以前(见底部)更健壮的新代码:
Update - here is the new code that is (should be) more robust than the previous (see bottom):
Public Class Form1
Private _originalSize As Size = Nothing
Private _scale As Single = 1
Private _scaleDelta As Single = 0.0005
Private Sub Form_MouseWheel(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'if very sensitive mouse, change 0.00005 to something even smaller
_scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
If e.Delta < 0 Then
_scale -= _scaleDelta
ElseIf e.Delta > 0 Then
_scale += _scaleDelta
End If
If e.Delta <> 0 Then _
PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)), _
CInt(Math.Round(_originalSize.Height * _scale)))
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'init this from here or a method depending on your needs
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Size = Panel1.Size
_originalSize = Panel1.Size
End If
End Sub
End Class
旧代码 - 有效,但可能由于 Scale() 中的舍入错误而在大的变化时不稳定:
Old code - works, but is unstable on large changes probably due to rounding errors in Scale():
Public Class Form1
Private _scale As New SizeF(1, 1)
Private _scaleDelta As New SizeF(0.01, 0.01) '1% for each wheel tick
Private Sub Form_MouseWheel(sender As System.Object,
e As MouseEventArgs) Handles Me.MouseWheel
'count incrementally
_scale.Height = 1
_scale.Width = 1
If e.Delta < 0 Then
_scale += _scaleDelta
ElseIf e.Delta > 0 Then
_scale -= _scaleDelta
End If
If e.Delta <> 0 Then _
PictureBox1.Scale(_scale)
End Sub
Private Sub Form1_Load(sender As System.Object,
e As EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'init picturebox size = image size
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Scale(New SizeF(1, 1))
PictureBox1.Size = PictureBox1.Image.Size
End If
End Sub
End Class
这篇关于如何在 vb.net 中使用滚轮放大图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!