[VB.Net/Graphics] Drawing arrows on the picturebox

  • Thread starter Thread starter kalp suth via DotNetMonster.com
  • Start date Start date
K

kalp suth via DotNetMonster.com

I want to create arrows using lines on a picture in the picture box.
On clicking the button "btnShowAll", the image is loaded and the lines drawn. "RGSShowAll()" calls "DrawObjs()" which does the actual drawing work.

But after displaying all the arrows in a flick, the arrows disappers.
I have debugged and came to know that the "Paint" event of the picturebox is refreshing the image, so the work done on the CreateGraphics created object is lost!

============== a trimmed-up code from the actual module...=============

Private bzrPen As New Pen(Color.Navy)
Private Location As String = "c:\test.jpg"

Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click

pcbMarkupImage.Image = CType(New Bitmap(Location), Image)
RGSShowAll(pcbMarkupImage)
End Sub

Public Sub RGSShowAll(ByVal PicBox As PictureBox)
Dim g As Graphics = Graphics.FromImage(i)

DrawObjs(g)

End Sub

Public Sub DrawObjs(ByRef objGrpahics As Graphics)
Try
Dim I, X1, Y1, X2, Y2 As Integer
' assigned values to the above line coordinates
X1 = 100
Y1 = 200
X2 = 150
Y2 = 300

objGrpahics.DrawLine(bzrPen, X1, Y1, X1, Y2)
objGrpahics.DrawLine(bzrPen, X1, Y1, X2, Y1)
objGrpahics.DrawLine(bzrPen, X2, Y2, X2, Y1)
objGrpahics.DrawString("title1", Me.Font, strBrush, x1 -5 , y1 + 5 )
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
====================================================================
 
this piece of code works for me, place it inside a button click event, what
it does is creates a bitmap based on the image in the picturebox, the it
draws the lines on this bitmap and place the new bitmap in the picturebox:

Dim g As Graphics
b = New Bitmap(PictureBox1.Image)
g = Graphics.FromImage(b)
g.DrawLine(Pens.Yellow, 100, 10, 110, 20)
g.DrawLine(Pens.Yellow, 110, 20, 100, 30)
g.DrawLine(Pens.Yellow, 90, 20, 110, 20)
PictureBox1.Image = b
g.Dispose()

hth Peter
 
Back
Top