I get no errors but when the program is debugged the code doesn't work, nothing happens. Can someone please tell me what I should do?
Thank You!

Private m_PanStartPoint As New Point

 Private Sub frmBirthdayCake_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
Panel2.AutoScroll = True
        'Picture Box Settings
        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
End Sub

 Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        m_PanStartPoint = New Point(e.X, e.Y)
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        'Verify Left Button is pressed while the mouse is moving
        If e.Button = Windows.Forms.MouseButtons.Left Then

            'Here we get the change in coordinates.
            Dim DeltaX As Integer = (m_PanStartPoint.X - e.X)
            Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y)

            'Then we set the new autoscroll position.
            'ALWAYS pass positive integers to the panels autoScrollPosition method
            Panel2.AutoScrollPosition = _
            New Drawing.Point((DeltaX - Panel2.AutoScrollPosition.X), _
                            (DeltaY - Panel2.AutoScrollPosition.Y))
        End If
    End Sub


Protected Overrides Sub DefWndProc(ByRef m As Message)
        If m.Msg <> 131 Then
            MyBase.DefWndProc(m)
        End If
    End Sub

For simple movement of your picture you can do this. No need of MouseDown event or DefwndProc or any difficult calculation.

Imports System
Imports System.Drawing


Public Class Form1


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Panel1.AutoScroll = True

        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
    End Sub


    Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Panel1.AutoScrollPosition = New System.Drawing.Point(Panel1.AutoScrollPosition.X + e.X, Panel1.AutoScrollPosition.Y + e.Y)
        End If

    End Sub
End Class

Edited 2 Years Ago by Shark_1

Comments
I want move the magnified/Zoomed picture too

I tried the above code but still it doesn't do anything.
First I want to zoom the picture then use the pan feature, can someone please help me on that.
Thank You!

Edited 2 Years Ago by Ashveen96: add more details

I tried that but I can't quite understand it. The image is retrieved from MS Access to the picture box, I want to Zoom and Pan the image in it, Can someone please tell me how??
Thank You!

I'm assuming... oddly once again you just want to pan the picturebox inside your panel, using the panel as a clipping device... Here, this is the panning code you need.

    Private mDown As Boolean = False
    Private oPoint As Point = Nothing
    Private offPoint As Point = Nothing

    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) 
        Handles PictureBox1.MouseDown

        If mDown = False Then
            mDown = True
            oPoint = New Point(Cursor.Position.X - (Panel1.Left + Me.Left),
                               Cursor.Position.Y - (Panel1.Top + Me.Top))

            offPoint = New Point(PictureBox1.Left, PictureBox1.Top)

        End If

    End Sub


    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) 
        Handles PictureBox1.MouseMove

        If mDown = True Then

            PictureBox1.Left = offPoint.X - (oPoint.X - (Cursor.Position.X -
                                                         (Panel1.Left + Me.Left)))

            PictureBox1.Top = offPoint.Y - (oPoint.Y - (Cursor.Position.Y -
                                                        (Panel1.Top + Me.Top)))

        End If

    End Sub

tried that nothing happens to the image in the picturebox, can someone please help me????

Ok, that's good...

Then it's you that's doing something wrong.

Download the solution below, it is the same code in action. I have modified it slightly to ensure the pan doesnt go beyond the boundries of the image.

I have to stress though, panning and zooming an image this way just isn't cool beans, however... it seems to be a popular aproach by... people.

Edited 2 Years Ago by J.C. SolvoTerra

You can write a function to scale the parent panel.

For example:

    Private Sub btnIn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnIN.Click
        'Increase the size of the picturebox, which increases the picture size.
        If Not IsNothing(Panel2) Then Panel2.Size = ScaleTenPercent(Panel2.Size, Zoom.zIn)
        If Not IsNothing(Panel2) Then Panel2.Refresh()
    End Sub

    Private Sub btnOut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOUT.Click
        'Decrease the size of the picturebox, which decreases the picture size.
        If Not IsNothing(Panel2) Then Panel2.Size = ScaleTenPercent(Panel2.Size, Zoom.zOut)
        If Not IsNothing(Panel2) Then Panel2.Refresh()
    End Sub

    Private Enum Zoom
        zIn = 1
        zOut = 2
    End Enum

    Private Function ScaleTenPercent(ByVal sizeIn As Size, ByVal mode As Zoom) As Size
        Try
            'Note the 1.1 and .9
            If mode = Zoom.zIn Then
                Dim newWidth As Integer = sizeIn.Width * 1.1
                Dim newHeight As Integer = sizeIn.Height / sizeIn.Width * newWidth
                Return New Size(newWidth, newHeight)
            ElseIf mode = Zoom.zOut Then
                Dim newWidth As Integer = sizeIn.Width * 0.9
                Dim newHeight As Integer = sizeIn.Height / sizeIn.Width * newWidth
                Return New Size(newWidth, newHeight)
            Else
                Return New Size(sizeIn.Width, sizeIn.Height)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            Return New Size(sizeIn.Width, sizeIn.Height)
        End Try
    End Function
This article has been dead for over six months. Start a new discussion instead.