none
How to zoom image in picturebox1 using button "+"\"-" by vb.net2005? RRS feed

  • Question

  • Hi all,
    I am using this code to zoom in\out using mousewheel.
    And now I want to know How to zoom image in picturebox1 using button "+"\"-" ?
    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
    
            'init this from here or a method depending on your needs
            If PictureBox1.Image IsNot Nothing Then
                PictureBox1.Size = Panel1.Size
                _originalSize = Panel1.Size
            End If
    
        End Sub

    Regards.....
    Friday, July 14, 2017 12:41 AM

Answers

  • Hi omanxp45-2,

    According to your description, you can refer to the code below.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click PictureBox1.Width = PictureBox1.Width * 1.5 PictureBox1.Height = PictureBox1.Height * 1.5 If PictureBox1.Height > 675 Then PictureBox1.Width = 335 PictureBox1.Height = 200 End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click PictureBox1.Width = PictureBox1.Width / 1.5 PictureBox1.Height = PictureBox1.Height / 1.5 If PictureBox1.Height < 17 Then PictureBox1.Width = 335 PictureBox1.Height = 200 End If End Sub Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load PictureBox1.Image = Image.FromFile("D:\Picture\1.Png") MessageBox.Show(PictureBox1.Width)

    End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, July 19, 2017 8:54 AM
    Moderator