Why drawing 2 Rectangles on a PictureBox by calling twice a "Function for Drawing" shows only the 2nd Rectangle?

VKSB 196 Reputation points
2024-05-14T11:01:48.48+00:00

Hi All,

Given below is my Function (a Sub) for drawing a "Rectangle" on a "PictureBox".

I wanted to draw 2 Rectangles side by side. When I called the function for each Rectangle, only the 2nd Rectangle was drawn, the 1st Rectangle was not drawn.

Could someone help me to make it work?

I can draw 2 Rectangles side by side by writing code for each Rectangle separately but not by calling a Function for drawing a Rectangle. I wonder why.

Here are the code:

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

Private Sub DrawRectanglesFunction_Btn_Click(sender As Object, e As EventArgs) Handles DrawRectanglesFunction_Btn.Click

        Draw_Rectangle_Function(PictureBox1, 25, 25, 75, 25, 25, 75, 75, 75)
        Draw_Rectangle_Function(PictureBox1, 100, 25, 150, 25, 100, 75, 150, 75)
    End Sub


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

       
        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

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,610 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 44,186 Reputation points Microsoft Vendor
    2024-05-14T13:04:38.51+00:00

    Hi,@VKSB .Welcome to Microsoft Q&A.

    In your Draw_Rectangle_Function subroutine, you are creating a new Bitmap and Graphics object every time it is called. This means that when you call it the second time, it replaces the existing image in the PictureBox with a new one, and only the second rectangle is drawn on the new image.

    To draw multiple rectangles on the same image, you should create the Bitmap and Graphics objects once and pass them to the subroutine. Here's how you can modify your code:

     Sub Draw_Rectangle_Function(ByRef Rectangles As Graphics, 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 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)
     End Sub
     Private Sub DrawRectanglesFunction_Btn_Click(sender As Object, e As EventArgs) Handles DrawRectanglesFunction_Btn.Click
         Dim BMP As New Bitmap(PictureBox1.Width, PictureBox1.Height)
         Dim Rectangles As Graphics = Graphics.FromImage(BMP)
         Draw_Rectangle_Function(Rectangles, 25, 25, 75, 25, 25, 75, 75, 75)
         Draw_Rectangle_Function(Rectangles, 100, 25, 150, 25, 100, 75, 150, 75)
         PictureBox1.Image = BMP
     End Sub
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more