Zooming drawn shapes

user20 161 Reputation points
2022-12-22T12:26:18.977+00:00

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.
2,737 questions
0 comments No comments
{count} votes

2 answers

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

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

    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  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.