Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Add zoom to picturebox

Hi,
How add a functionality of zoom in and out on picturebox in vb.net (desktop).
Code for adding image to picturebox
 Protected Friend Sub ScaleImage(ByVal p As PictureBox, ByVal i As Bitmap)
        Try
         
        
            p.SizeMode = PictureBoxSizeMode.AutoSize
            p.Image = i

            FrmMain.Panel1.AutoScroll = True


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try




    End Sub

Open in new window

Avatar of Misha
Misha
Flag of Russian Federation image

Try this code:
Public Class Form1

    Private _originalSize As Size = Nothing
    Private _scale As Single = 1
    Private _scaleDelta As Single = 0.0005

    Private Sub Form_MouseWheel(sender As System.Object, 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(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        If PictureBox1.Image IsNot Nothing Then
            PictureBox1.Size = Panel1.Size
            _originalSize = Panel1.Size
        End If
    End Sub
End Class

Open in new window


Source:
https://stackoverflow.com/questions/13496706/how-to-zoom-in-a-picturebox-with-scrollwheel-in-vb-net
Hi RIAS,

Are you using Winforms or WPF? Also you want to Zoom In/Out using Mouse wheel or buttons?

Regards,
Chinmay.
Avatar of RIAS

ASKER

Hi,
I am using winforms  vb.net desktop 2017.
On a button click it willbe great!
Avatar of RIAS

ASKER

Misha,Thanks that code doesnt work for me.
What does not work? What is the errors?
Avatar of RIAS

ASKER

The image just disappear on mouse roll event
Set properties
 PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Panel1.AutoScroll = True

Open in new window

 and try this code with  buttons:
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox1.Height *= 1.2
        PictureBox1.Width *= 1.2
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PictureBox1.Height *= 0.8
        PictureBox1.Width *= 0.8
    End Sub

    Private Sub PictureBox1_LoadCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles PictureBox1.LoadCompleted
        PictureBox1.Height = PictureBox1.Image.Height
        PictureBox1.Width = PictureBox1.Image.Width
    End Sub
End Class

Open in new window

Avatar of RIAS

ASKER

Misha! Trying brb
Avatar of RIAS

ASKER

Misha,
It works but it keeps changing the position.
Avatar of RIAS

ASKER

how can I define  the picturebox location.
ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Misha,
Stretch looks horrible.
I will try location