Zooming drawn shapes

asked 2022-12-22T12:26:18.977+00:00
user20 141 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.
273343-size-rectangle.gif
The PictureBoxSizeMode is StretchImage.
I put my codes. Please guide me.

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

2 answers

Sort by: Most helpful
  1. answered 2022-12-22T14:50:02.587+00:00
    LesHay 5,661 Reputation points

    Hi
    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. answered 2022-12-24T13:27:15.697+00:00
    LesHay 5,661 Reputation points

    Hi
    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  
        PictureBox1.Invalidate()  
      End Sub  
    End Class  
    
    No comments