如何使用vb.net中的滚轮放大一个Picturebox
我使用一组图形叠加层来使用图形对象在图片框控件内绘制图像。 我已将Picturebox放置在Panel中并将Panel设置为自动滚动。 我现在需要知道如何做的是使用鼠标滚轮以较小的增量放大图片的大小,同时保持绘制图像的质量。 有人知道怎么做吗?
当我使用下面的Abdias软件代码更新时,当picturebox的Sizemode属性设置为StretchImage时,图片开始变小。 我有一个鼠标泛功能,可能会干扰这些代码无法正常工作。 有任何想法吗? 有什么可以保持这个工作不正常?
解决了
这段代码对我来说比以下任何一个都好得多:
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
( Panel1
内的PictureBox1
, Panel1.AutoScroll = True
)并在PictureBox
上设置了一个图像。
该代码不会计算缩放的中心点,但您可以使用e.Location(或eX / eY)。
更新 - 这里是新的代码(应该是)比以前更强大(请参阅底部):
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()中的舍入错误而不稳定:
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
基本上,你需要一个图像查看器。 我之前使用过这个:http://cyotek.com/blog/creating-a-scrollable-and-zoomable-image-viewer-in-csharp-part-4
它的效果很好。 但是,它是一个用户控件。
对于picturebox,您需要从图像创建图形,然后对其进行插值。 这里是一个例子:http://www.dotnetcurry.com/ShowArticle.aspx?ID=196
我没有检查这个,但看起来会工作。
我注意到StretchImage
SizeMode
忽略图像比例会产生不良影响。 我只是添加了一个宽度和高度比率变量,以包含在“缩放”算法中。 请参阅下面的代码中的_ratWidth
和_ratHeight
。
Public Class Form1
Private _originalSize As Size = Nothing
Private _scale As Single = 1
Private _scaleDelta As Single = 0.0005
Private _ratWidth, _ratHeight As Double
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 * _ratWidth) * _scale)), _
CInt(Math.Round((_originalSize.Height * _ratHeight) * _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
_ratWidth = PictureBox1.Image.Width / PictureBox1.Image.Height
_ratHeight = PirctureBox1.Image.Height / PictureBox1.Image.Width
PictureBox1.Size = Panel1.Size
_originalSize = Panel1.Size
End If
End Sub
End Class
链接地址: http://www.djcxy.com/p/66245.html