You can try this code. It is assumed that in the form ( PictureBox1 inside Panel1.AutoScroll = True with Panel1.AutoScroll = True ) there is Panel1 and PictureBox1 ) with the image installed on the PictureBox .
The code does not calculate the central point of scaling, but you can use e.Location (or eX / eY) for this.
Update - here is the new code, which (should be) more reliable than the previous one (see below):
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
Old code - it works, but is unstable with 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
K3N
source share