Hello everybody!
I’m trying to create a program in which the user has the ability to move the image with mouse click and ability to zoom in and out by scrolling the mouse wheel. If the user wants to return to the original image size and position, it will be possible with a click of a button.
I was able to do everything said above for the exception that after the user clicks on the reset button and zooms again, the previous size of the image is shown with the position where it was before the reset button was pressed.
How would it be possible to fix this without recoding the whole thing?
And also can you tell me how to make the image to zoom in to center of the mouse pointer, like it does in Google maps, the map zooms to the center of location of the mouse pointer.
This is the code which I got so far:
Public Class Form1
Private _originalSize As Size = Nothing
Private _scale As Single = 2
Private _scaleDelta As Single = 0.0001
Private allowCoolMove As Boolean = False
Private myCoolPoint As New Point
Dim OriginalPic As Size
Private Sub Form_MouseWheel(ByVal sender As System.Object, ByVal 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
'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
'This code allows the image to be moved around
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
allowCoolMove = True
myCoolPoint = New Point(e.X, e.Y)
Me.Cursor = Cursors.SizeAll
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If allowCoolMove = True Then
PictureBox1.Location = New Point(PictureBox1.Location.X + e.X - myCoolPoint.X, PictureBox1.Location.Y + e.Y - myCoolPoint.Y)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
allowCoolMove = False
Me.Cursor = Cursors.Default
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Resets the image size to original size
PictureBox1.Size = New Size(555, 312)
'Resets the image position to original position
PictureBox1.Location = New Point(0, 0)
End Sub
End Class