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