Postman
Postman

Reputation: 517

Vb.net picturebox zooming

I am attempting to replicate windows photo viewer, to an extent.

Right now I have a form with a picturebox, and the ability to move it around with the mouse and zoom in/out with the scroll wheel.

However, I wish to zoom towards the mouse pointer. You can see what I am trying to explain by opening a decently big image in windows photo viewer, and zooming with your mouse somewhere away from the center of the image. I want to replicate that, but so far I can only zoom in and out.

I know that I have to move the image in the opposite direction of the mouse pointer to the center of the form, and vary how much it moves per scroll wheel tick depending on how far your mouse is from the center of the form, but this is where I'm stuck.

Here's my laughable, confused piece of code that's half commented out and partway between not working and completely not working:

    Dim Me_Center As Point = New Point(Me.Width / 2, Me.Height / 2)
    Dim PB_Center_R As Point = New Point(PictureBox1.Width / 2, PictureBox1.Height / 2)
    Dim PB_Center As Point = New Point(PictureBox1.Location.X + PB_Center_R.X, PictureBox1.Location.Y + PB_Center_R.Y)
    Dim PB_Diff As Point = (PB_Center - MousePos)


    PictureBox1.Location = New Point((Me_Center - PB_Center_R) - PB_Diff)

    'PictureBox1.Location = New Point((Me.Width / 2) - (PictureBox1.Width / 2), (Me.Height / 2) - (PictureBox1.Height / 2))

    '(Me.Width / 2) - (PictureBox1.Width + Pos.X / 2), (Me.Height / 2) - (PictureBox1.Height - Pos.Y / 2)
    'PictureBox1.Location = New Point((Me.Width / 2 - (PictureBox1.Width / 2)) + XP, (Me.Height / 2 - (PictureBox1.Height / 2)) - YP)

this does pretty much exactly what I want (if you hit the "Open Zoomable Image" when running this form, however I can't understand exactly how it works: http://www.vbforums.com/showthread.php?654846-ZoomPictureBox-picture-control-with-mouse-centred-zooming

Upvotes: 3

Views: 6172

Answers (1)

SysDragon
SysDragon

Reputation: 9888

You can resize the PictureBox that contains the image the way you want. Set the SizeMode to Zoom so the image readjust its size automatically:

PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom

And then resize the PictureBox as you want.

You can detect the mouse position in the form by adding the event MouseMove on the form and storing the position in a variable. Or you can get the mouse position in the screen at any moment with:

Dim p As Point = Me.PointToClient(Cursor.Position)

You can read a similar question here: How to zoom in a Picturebox with scrollwheel in vb.net

Upvotes: 1

Related Questions