如何在vb.net中使用滚轮放大Picturebox

前端之家收集整理的这篇文章主要介绍了如何在vb.net中使用滚轮放大Picturebox前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一组图形叠加来使用图形对象在图片框控件内绘制图像.我已将PictureBox放在Panel中,并将Panel设置为自动滚动.我现在需要知道的是使用鼠标滚轮以小幅度增大图片的大小,同时保持绘制图像的质量.有人知道怎么做吗?

当我使用下面的Abdias软件代码更新时,当pictureBox的Sizemode属性设置为StretchImage时,图片开始变小.我有一个使用鼠标的平移功能可能会干扰保持此代码无法正常工作.有任何想法吗?有什么可以阻止它正常工作?

解决

这段代码对我来说比以下任何一个都好得多:

  1. Private Sub PictureBox_MouseWheel(sender As System.Object,e As MouseEventArgs) Handles PictureBox1.MouseWheel
  2. If e.Delta <> 0 Then
  3. If e.Delta <= 0 Then
  4. If PictureBox1.Width < 500 Then Exit Sub 'minimum 500?
  5. Else
  6. If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000?
  7. End If
  8.  
  9. PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
  10. PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
  11. End If
  12.  
  13. End Sub
您可以尝试此代码.假设窗体上存在Panel1和PictureBox1(Panel1内的PanelBox1,Panel1.AutoScroll = True),PictureBox上设置了图像.

代码不计算缩放的中心点,但您可以使用e.Location(或e.X / e.Y).

更新 – 这里是(应该)比以前更强大的新代码(见底部):

  1. Public Class Form1
  2.  
  3. Private _originalSize As Size = Nothing
  4. Private _scale As Single = 1
  5. Private _scaleDelta As Single = 0.0005
  6.  
  7. Private Sub Form_MouseWheel(sender As System.Object,e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
  8.  
  9. 'if very sensitive mouse,change 0.00005 to something even smaller
  10. _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
  11.  
  12. If e.Delta < 0 Then
  13. _scale -= _scaleDelta
  14. ElseIf e.Delta > 0 Then
  15. _scale += _scaleDelta
  16. End If
  17.  
  18. If e.Delta <> 0 Then _
  19. PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)),_
  20. CInt(Math.Round(_originalSize.Height * _scale)))
  21.  
  22. End Sub
  23.  
  24. Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load
  25. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
  26.  
  27. 'init this from here or a method depending on your needs
  28. If PictureBox1.Image IsNot Nothing Then
  29. PictureBox1.Size = Panel1.Size
  30. _originalSize = Panel1.Size
  31. End If
  32.  
  33. End Sub
  34.  
  35. End Class

代码 – 工作,但在大的更改中不稳定可能是由于Scale()中的舍入错误

  1. Public Class Form1
  2.  
  3. Private _scale As New SizeF(1,1)
  4. Private _scaleDelta As New SizeF(0.01,0.01) '1% for each wheel tick
  5.  
  6. Private Sub Form_MouseWheel(sender As System.Object,e As MouseEventArgs) Handles Me.MouseWheel
  7. 'count incrementally
  8. _scale.Height = 1
  9. _scale.Width = 1
  10.  
  11. If e.Delta < 0 Then
  12. _scale += _scaleDelta
  13. ElseIf e.Delta > 0 Then
  14. _scale -= _scaleDelta
  15. End If
  16.  
  17. If e.Delta <> 0 Then _
  18. PictureBox1.Scale(_scale)
  19.  
  20. End Sub
  21.  
  22. Private Sub Form1_Load(sender As System.Object,e As EventArgs) Handles MyBase.Load
  23.  
  24. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
  25.  
  26. 'init pictureBox size = image size
  27. If PictureBox1.Image IsNot Nothing Then
  28. PictureBox1.Scale(New SizeF(1,1))
  29. PictureBox1.Size = PictureBox1.Image.Size
  30. End If
  31.  
  32. End Sub
  33.  
  34. End Class

猜你在找的VB相关文章