Zooming drawn shapes

user20 161 Reputation points

Hi dears
I have a picturebox that draw shape like rectangle by mouseclick on the picturebox. and have two button for zoom-in and zoom-out the image of picturebox and drawn shapes. Buttons working correctly about image but I can't zooming the drawn shapes.
The PictureBoxSizeMode is StretchImage.
I put my codes. Please guide me.

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,981 questions
No comments
{count} votes

2 answers

Sort by: Oldest
  1. LesHay 6,756 Reputation points

    I use this to scale drawn items:

    In the Paint Event for the object, I have:

    	Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint  
    		e.Graphics.ScaleTransform(My.Settings.sc, My.Settings.sc)  

    and as can be seen, the scale parameter is stored in a My.Settings variable to keep value over rerunning application. In code, I have a MouseWheel event handler where the scaling variable is changed either up/down depending on mouse wheel direction (e.delta), and kept within suitable upper and lower values. As the scaling variable is changed, an Invalidate is issued to force a redraw for the container - end result is real-time scaling.

  2. LesHay 6,756 Reputation points

    Here is a very simple example. This is a stand alone example and if you want to try it out, start a new test project, put a PictureBox1 on Form1 in the Designer and Dock (full). Copy/replace all Form1 code with this code and run. The Mouse Wheel will scale up/down.

    ' Form1 with Docked PictureBox1  
    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Dim Gscale As Single = 1S  
      Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint  
        e.Graphics.ScaleTransform(Gscale, Gscale)  
        With e.Graphics  
          Using p As New Pen(Color.Red, 4)  
            .DrawRectangle(p, 10, 10, 100, 50)  
          End Using  
          Using p As New Pen(Color.Blue, 2)  
            .DrawRectangle(p, 20, 20, 50, 100)  
          End Using  
          Using p As New SolidBrush(Color.Green)  
            .FillRectangle(p, 90, 40, 50, 100)  
          End Using  
          Using p As New Pen(Color.Yellow, 1)  
            .DrawEllipse(p, 92, 42, 46, 96)  
          End Using  
          Using p As New SolidBrush(Color.Violet)  
            .FillEllipse(p, 24, 80, 24, 24)  
          End Using  
        End With  
      End Sub  
      Private Sub PictureBox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseWheel  
        Select Case e.Delta  
          Case > 0  
            Gscale += 0.1D  
            If Gscale > 5 Then Gscale = 5  
          Case < 0  
            Gscale -= 0.1D  
            If Gscale < 0.75 Then Gscale = 0.75  
        End Select  
      End Sub  
    End Class