Greg Willard
Greg Willard

Reputation: 159

How would you keep from panning past the edges of a PictureBox control nested inside a Panel while zooming?

I would like to know how I can alter my code to keep from panning past the edges of the PictureBox while it is zoomed in and when it's in normal state. If possible I would also like to know how to make it zoom at the mouse's current location it is hovering and keep the image quality while zooming. Any Help would greatly be appreciated.

Here is the code for zooming:

Private Sub PictureBox_MouseWheel(sender As System.Object,
                             e As MouseEventArgs) Handles PictureBox1.MouseWheel
    If e.Delta <> 0 Then
        If e.Delta <= 0 Then
            If PictureBox1.Width < 500 Then Exit Sub 
        Else
            If PictureBox1.Width > 2000 Then Exit Sub 
        End If

        PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
        PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
    End If

End Sub

And here is the code for Panning I am using:

Private Offset As Point
    Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Offset = e.Location
    End Sub
    Private Sub Picturebox1_MouseMove(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    PictureBox1.Select()
    If e.Button = Windows.Forms.MouseButtons.Left Then
        PictureBox1.Left += e.X - Offset.X
        PictureBox1.Top += e.Y - Offset.Y
    End If
    End Sub

Upvotes: 3

Views: 1089

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26434

Your code is doing drag&drop instead of panning, and resize instead of zooming. If you want to pan and zoom, check these question on Stackoverflow:

Also check this:

Creating a scrollable and zoomable image viewer in C# Part 4

and this:

PictureBox with zooming and scrolling - a working project, however, it does not pan with MouseMove, so you need to use a scrollbar. Zoom works. Language is C#. Written in VS 2003 - needs conversion.

Upvotes: 1

Related Questions