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
    Private Sub Form_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
        If e.Delta < 0 Then
            PictureBox1.Size = New Size(PictureBox1.Size.Width - 10, PictureBox1.Size.Height - 10)
        ElseIf e.Delta > 0 Then
            PictureBox1.Size = New Size(PictureBox1.Size.Width + 10, PictureBox1.Size.Height + 10)
        End If
    End Sub

Thank you oissama 1 for your reply. The code works and zooms in and out to the center of the panel, however, I want the image to zoom in and out to the center of the current mouse cursor position. How would this be possible? Anyone?

i'm assuming the picture is inside the panel

    Private Sub Form_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
        If e.Delta < 0 Then
            PictureBox1.Size = New Size(PictureBox1.Size.Width - 10, PictureBox1.Size.Height - 10)

        ElseIf e.Delta > 0 Then
            PictureBox1.Size = New Size(PictureBox1.Size.Width + 10, PictureBox1.Size.Height + 10)
            Dim mousep As Point = e.Location
            Dim panelcenter_w As String = Panel1.Size.Width / 2
            Dim panelcenter_h As String = Panel1.Size.Height / 2
            If mousep.X < panelcenter_w Then
                Dim centringx As String = panelcenter_w - (mousep.X)
                centringx = centringx / 12
                PictureBox1.Location = New Point(PictureBox1.Location.X + centringx, PictureBox1.Location.Y)
            Else
                Dim centringx As String = (mousep.X) - panelcenter_w
                centringx = centringx / 12
                PictureBox1.Location = New Point(PictureBox1.Location.X - centringx, PictureBox1.Location.Y)
            End If
            If mousep.Y < panelcenter_h Then
                Dim centringy As String = panelcenter_h - (mousep.Y)
                centringy = centringy / 12
                PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + centringy)
            Else
                Dim centringy As String = (mousep.Y) - panelcenter_h
                centringy = centringy / 12
                PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - centringy)
            End If
        End If
    End Sub

oussama_1 thank you for your response. After I've tested the code above, the image was moving in awkward way but not to the center of the location of the cursor as I'm trying to have for the outcome. Will try some more research, which doesn't always come to success.

I found this code, but there seems to be syntax errors. Wonder how could I fix this?

Private Declare Function GetWindowRect Lib "user32" (ByVal HWND As Long, lpRect As RECT) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type
Private Type POINTAPI
  x As Long
  y As Long
End Type

Public Sub ZoomCustom(dRatio As Double)
  Dim tRect As RECT
  Dim tPoint As POINTAPI
  Dim ext As MapWinGIS.Extents
  Dim dWidth As Double
  Dim dHeight As Double
  Dim dXCent As Double
  Dim dYCent As Double
  Dim dx As Double
  Dim dy As Double

    ' the position of mouse cursor - the center of new extent
    GetWindowRect mAxMap.HWND, tRect
    GetCursorPos tPoint
    dx = tPoint.x - tRect.Left
    dy = tPoint.y - tRect.Top
    mAxMap.PixelToProj dx, dy, dXCent, dYCent

    ' determine the size of new extent
    Set ext = mAxMap.Extents
    dHeight = (ext.yMax - ext.yMin) * dRatio
    dWidth = (ext.xMax - ext.xMin) * dRatio

    ' apply new extent
    Set ext = New MapWinGIS.Extents
    ext.SetBounds dXCent - dWidth / 2, dYCent - dHeight / 2, 0, dXCent + dWidth / 2, dYCent + dHeight / 2, 0
    Set mAxMap.Extents = ext
    mAxMap.Redraw

    ' return cursor to the same position on map
    mAxMap.ProjToPixel dXCent, dYCent, dx, dy
    SetCursorPos tRect.Left + CLng(dx), tRect.Top + CLng(dy)
End Sub

I'm playing around and I came up with this. Create a form and place a PictureBox in Normal SizeMode.

Dim original As Image
Dim imgscale As Double = 1.0

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    original = PictureBox1.Image
End Sub

Private Sub Form1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel

    Select Case True
        Case e.Delta > 0 : imgscale *= 1.1
        Case e.Delta < 0 : imgscale *= 0.9
        Case e.Delta = 0 : Exit Sub
    End Select

    Dim BiggerImage As Bitmap
    BiggerImage = New Bitmap(original, original.Width * imgscale, original.Height * imgscale)
    PictureBox1.Height = BiggerImage.Height
    PictureBox1.Width = BiggerImage.Width
    PictureBox1.Image = BiggerImage

End Sub

Reverend Jim, I've tried the code, but still, this doesn't give me what I'm looking for. The image doesn't zoom to the center of the cursor postion.

I haven't had a chance to get to that part of it yet but perhaps there is a way to orient the picture in the picturebox taking the cursor position into account.

I've looked all over the web, but still couldn't find the solution. Where could I find the code which does what I need?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.