A community member has associated this post with a similar question:
Why drawing 2 Rectangles on a PictureBox by calling twice a "Function for Drawing" shows only the 2nd Rectangle?

Only moderators can edit this content.

Why two "Rectangle" can't be drawn on a PictureBox calling a "Function for Drawing" 2nd time?

VKSB 196 Reputation points
2024-05-14T08:56:45.03+00:00

Hi All,

I wrote a Function (Sub) for drawing a "Rectangle" on a "Picture Box" as given below.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

'Calling the Function.

 ' 1st Rectangle.
        'Draw_Rectangle_Function(PictureBox1, 25, 25, 75, 25, 25, 75, 75, 75)

 ' 2nd Rectangle.
        'Draw_Rectangle_Function(PictureBox1, 100, 25, 150, 25, 100, 75, 150, 75)


        Dim BMP As Bitmap
        Dim Rectangle_1, Rectangle_2 As Graphics
        BMP = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Image = BMP
        Rectangle_1 = Graphics.FromImage(BMP)
        Rectangle_2 = Graphics.FromImage(BMP)
        Dim PenColor1 As New System.Drawing.Pen(System.Drawing.Color.Red)

        '1st Rectangle
        Rectangle_1.DrawLine(PenColor1, 25, 25, 75, 25)
        Rectangle_1.DrawLine(PenColor1, 25, 75, 75, 75)
        Rectangle_1.DrawLine(PenColor1, 25, 25, 25, 75)
        Rectangle_1.DrawLine(PenColor1, 75, 25, 75, 75)

        Rectangle_1.Dispose()

        '2nd Rectangle
        Rectangle_2.DrawLine(PenColor1, 100, 25, 150, 25)
        Rectangle_2.DrawLine(PenColor1, 100, 75, 150, 75)
        Rectangle_2.DrawLine(PenColor1, 100, 25, 100, 75)
        Rectangle_2.DrawLine(PenColor1, 150, 25, 150, 75)

        Rectangle_2.Dispose()
    End Sub


'Function (Sub) for Drawing a Rectangle.

Sub Draw_Rectangle_Function(ByRef PictureBox As PictureBox, ByRef X1 As Integer, ByRef Y1 As Integer, ByRef X2 As Integer, ByRef Y2 As Integer, ByRef X3 As Integer, ByRef Y3 As Integer, ByRef X4 As Integer, ByRef Y4 As Integer)

        Dim BMP As Bitmap
        Dim Rectangles As Graphics

        BMP = New Bitmap(PictureBox.Width, PictureBox.Height)
        PictureBox.Image = BMP
        Rectangles = Graphics.FromImage(BMP)
        Dim PenColor1 As New System.Drawing.Pen(System.Drawing.Color.Red)

        Rectangles.DrawLine(PenColor1, X1, Y1, X2, Y2)
        Rectangles.DrawLine(PenColor1, X3, Y3, X4, Y4)
        Rectangles.DrawLine(PenColor1, X1, Y1, X3, Y3)
        Rectangles.DrawLine(PenColor1, X2, Y2, X4, Y4)

        Rectangles.Dispose()
    End Sub

I wrote "Imports System.Drawing.Graphics" at the top of the Form1.

Calling the Function twice to draw 2 Rectangles shows only the 2nd Rectangle; but after commenting out the Function calling and run the project with the Code for drawing 2 Rectangles as given above draws 2 Rectangles.

Could you please let me know why calling the "Function" to draw 2 Rectangles shows only the 2nd Rectangle?

How can I make both Rectangles drawn?

Thanks

VKSB

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

1 answer

Sort by: Most helpful
  1. Viorel 113.7K Reputation points
    2024-05-14T09:16:45.9233333+00:00

    Try an adjusted procedure:

    Sub Draw_Rectangle_Function(ByRef PictureBox As PictureBox, ByRef X1 As Integer, ByRef Y1 As Integer, ByRef X2 As Integer, ByRef Y2 As Integer, ByRef X3 As Integer, ByRef Y3 As Integer, ByRef X4 As Integer, ByRef Y4 As Integer)
    
        Dim BMP As Image = If(PictureBox.Image, New Bitmap(PictureBox.Width, PictureBox.Height))
    
        Using Rectangles = Graphics.FromImage(BMP)
            Using PenColor1 As New Pen(Color.Red)
    
                Rectangles.DrawLine(PenColor1, X1, Y1, X2, Y2)
                Rectangles.DrawLine(PenColor1, X3, Y3, X4, Y4)
                Rectangles.DrawLine(PenColor1, X1, Y1, X3, Y3)
                Rectangles.DrawLine(PenColor1, X2, Y2, X4, Y4)
    
            End Using
        End Using
    
        PictureBox.Image = BMP
        PictureBox.Invalidate()
    End Sub
    

    The bitmap, which already contains a partial image, is reused by this code.

    0 comments No comments