I'm trying to zoom in on a Picturebox when scrolling the mousewheel, the code below zooms in on the image only to the center of the image, not at the current cursor position. How would I fix this?

PictureBox1.Size = New System.Drawing.Size(PictureBox1.Width + e.Delta / 1, PictureBox1.Height + e.Delta / 1)
PictureBox1.Location = New Point(Control.MousePosition.X - PictureBox1.Width / 2, Control.MousePosition.Y - PictureBox1.Height / 2)

Dani AI

Generated

The root cause is using screen coordinates (for example via Control.MousePosition) and then re-centering the PictureBox against the screen. That forces the control to always appear to zoom toward the center instead of the pixel under the cursor. Keep the image point under the cursor fixed by: get the mouse location relative to the PictureBox, compute the relative anchor (rx, ry), change the zoom, resize the PictureBox to the scaled image size, then shift the control by the scaled delta so the same image point stays under the mouse. This avoids pulling in external libs — and offered existing controls and a DLL, but a few lines in your project are enough.

The more reliable place to handle wheel events is the form (or the parent scrollable container) so you do not depend on the PictureBox being focusable. Example (VB.NET):

' Form-level field
Private _zoom As Single = 1.0F

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    ' PictureBox1.Image = ... load your image
End Sub

Private Sub Form1_MouseWheel(sender As Object, e As MouseEventArgs) Handles MyBase.MouseWheel
    If PictureBox1.Image Is Nothing Then Return
    Dim oldW = PictureBox1.Width, oldH = PictureBox1.Height
    If oldW = 0 Or oldH = 0 Then Return
    Dim pt = PictureBox1.PointToClient(Control.MousePosition)
    If Not PictureBox1.ClientRectangle.Contains(pt) Then Return
    Dim rx = pt.X / CDbl(oldW), ry = pt.Y / CDbl(oldH)
    Dim steps = e.Delta / 120.0
    Dim factor = Math.Pow(1.1, steps)
    _zoom = Math.Max(0.05F, Math.Min(20.0F, CSng(_zoom * factor)))
    Dim newW = CInt(PictureBox1.Image.Width * _zoom), newH = CInt(PictureBox1.Image.Height * _zoom)
    PictureBox1.Size = New Size(newW, newH)
    PictureBox1.Left -= CInt((newW - oldW) * rx)
    PictureBox1.Top  -= CInt((newH - oldH) * ry)
End Sub

Notes: clamp zoom to sane limits, check for Image = Nothing and zero sizes, and if the PictureBox lives inside an AutoScroll Panel update the panel's AutoScrollPosition rather than moving the child. This explanation ties back to 's original symptom (zooming to center) and offers a robust fix without external libraries.

Recommended Answers

All 6 Replies

As is typical with sample code/controls like the one referenced in the previous post, it is written in an older version of Visual Studio. Loading it into Visual Studio 2010 (which is what I am using) results in nothing usable. There are also no instructions on how to rebuild the demo project. Trying to rebuild in even newer versions (2012, 2013) is likely to lead to nothing but frustration.

Yes I agree that this will

lead to nothing but frustration

I tried to run this program, but couldn't extract any foundation where to start from.

My bad. Thought it would be easier to change the code. I use vb 13 express and no you just can't expect to import and run the code - it needs to get modified. I will give it aa try.

I found this code but I wasn't able to make it work in my program.

Inherits PictureBox

 Overloads Property Image() As Image ' we want to hook when client's set the image to reset the zoom level to unzoomed
        Get
            Return MyBase.Image
        End Get
        Set(ByVal value As Image)
            zmLevel = 1 ' reinit
            MyBase.Image = value
        End Set
    End Property

The code above wouldn't compile properly, and I'm not sure how to fix the issue. Tried to make research, but no success on finding any good examples of code.

I was able to convert the lib to vb 2010 and build it. Try taking the attached dll and dropping it on the toolbox in visual studio. I've used it in a couple of projects and it works just fine. It has the same properties/methods as a picturebox with a few extras.

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.