Draw a Line on a Picturebox Control in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)

The source code snip below will draw a line and be persisted onto a picturebox control using Visual Basic.NET and Higher including VB 2008 and 2010.

'Draws a line on picturebox control
'Put a picturebox on the form named pic
Dim bit As Bitmap = New Bitmap(pic.Width, pic.Height)

Dim g As Graphics = Graphics.FromImage(bit)

Dim myPen As Pen = New Pen(Color.Blue, 3)

g.DrawLine(myPen, 0, 0, pic.Width, pic.Height)

pic.Image = bit

2 thoughts on “Draw a Line on a Picturebox Control in VB.NET

  1. Donna

    When you put another window over it… the graphics are destroyed.

    Go code example. As long as you only open/run 1 program on your machine all week.

  2. Jason Post author

    I just tried that exact code and it worked perfectly fine. The image is tied to the picturebox controls .image property so it doesn’t erase when the picturebox redraws its contents. I didn’t have any kind of problem with that source code above.

    You could also put code in the _Paint event of the control and draw that way and the graphics will persist that way as well.



Leave a Reply to Donna Cancel reply