• 0

[VB.Net] drawing graphics with form .load type events


Question

i'm having trouble drawing some graphics, and i've been unable so far to find solutions to the following problems

1) displaying the initial image - when the form is displayed you see the image for a split second, but the imagebox object is immediately overwritten with a white background. i believe the problem is due to having put the drawing code in a function hooked to the forms load event, the code is being run before the form is displayed, and then once it is, for some reason the contents of the image box is wiped clean. i suppose i need to find a different event to hook onto so that the drawing is done after the form has been fully displayed, but what... i've tried the paint event but that didn't work...

2) i also need to have it redisplay the image after the form regains focus (it seems to loose it for some reason after loosing focus...). i hooked a function up to the paint event for the image box, but that didn't seem to have any effect...

thanks in advance for any help ;)

p.s. yes, this is college work incase anyone wonders, however i've already done and gone beyond what i was supposed to do, this is just me trying to squish some bugs to improve my code :p

Public Class frmDrawCat
	Dim pictureBox As Graphics
	Private iCatBoxValues() As Integer = {20, 20, 140, 100}

	Private Sub frmDrawCat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'assign graphics variable to image box
		pictureBox = imgPicBox.CreateGraphics

		'draw the initial cat
		drawCat()

		'setup text boxes with default values used above
		txtX.Text = iCatBoxValues(0)
		txtY.Text = iCatBoxValues(1)
		txtWidth.Text = iCatBoxValues(2)
		txtHeight.Text = iCatBoxValues(3)
	End Sub

	Private Sub imgPicBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles imgPicBox.Paint
		're-draw the cat in an event such as form re-focus
		drawCat()
	End Sub

	Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
		'capture input (co-ordinates, and dimentions)
		iCatBoxValues(0) = Integer.Parse(txtX.Text)
		iCatBoxValues(1) = Integer.Parse(txtY.Text)
		iCatBoxValues(2) = Integer.Parse(txtWidth.Text)
		iCatBoxValues(3) = Integer.Parse(txtHeight.Text)

		'draw the cat
		drawCat()
	End Sub

	Private Sub drawCat()
		'get co-ordinates & sizes
		Dim iX As Integer = iCatBoxValues(0)
		Dim iY As Integer = iCatBoxValues(1)
		Dim iWidth As Integer = iCatBoxValues(2)
		Dim iHeight As Integer = iCatBoxValues(3)

		'clear graphics area
		pictureBox.Clear(Color.White)

		'create pens for drawing
		Dim blackPenW1 As New Pen(Color.Black, 1)
		Dim blackPenW2 As New Pen(Color.Black, 2)
		Dim blackPenW3 As New Pen(Color.Black, 3)
		Dim blackPenSolid As New SolidBrush(Color.Black)

		'draw cat graphics objects
		'- head
		pictureBox.FillEllipse(New SolidBrush(Color.Orange), CInt(iX + ((iWidth / 14) * 3)), CInt(iY + ((iHeight / 10) * 2)), CInt((iWidth / 14) * 8), CInt((iHeight / 10) * 8))
		'- eyes
		pictureBox.FillEllipse(blackPenSolid, CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 5)), CInt((iWidth / 14)), CInt((iHeight / 10)))
		pictureBox.FillEllipse(blackPenSolid, CInt(iX + ((iWidth / 14) * 8)), CInt(iY + ((iHeight / 10) * 5)), CInt((iWidth / 14)), CInt((iHeight / 10)))
		'- nose
		pictureBox.DrawLine(blackPenW2, CInt(iX + (iWidth / 2)), CInt(iY + ((iHeight / 10) * 6)), CInt(iX + (iWidth / 2)), CInt(iY + ((iHeight / 10) * 8)))
		Try 'block errors trying to create arcs with values that are too small
			pictureBox.DrawArc(blackPenW2, CInt(iX + ((iWidth / 14) * 6)), CInt(iY + ((iHeight / 10) * 7)), CInt(((iWidth / 14) * 1)), CInt(((iHeight / 10) * 1)), 0, 180)
			pictureBox.DrawArc(blackPenW2, CInt(iX + ((iWidth / 14) * 7)), CInt(iY + ((iHeight / 10) * 7)), CInt((iWidth / 14) * 1), CInt((iHeight / 10) * 1), 0, 180)
		Catch ex As Exception
			'do nothing!
		End Try
		'- wiskers
		pictureBox.DrawLine(blackPenW3, CInt(iX), CInt(iY + ((iHeight / 10) * 6)), CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 7)))
		pictureBox.DrawLine(blackPenW3, CInt(iX), CInt(iY + ((iHeight / 10) * 9)), CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 8)))
		pictureBox.DrawLine(blackPenW3, CInt(iX + ((iWidth / 14) * 9)), CInt(iY + ((iHeight / 10) * 7)), CInt(iX + iWidth), CInt(iY + ((iHeight / 10) * 6)))
		pictureBox.DrawLine(blackPenW3, CInt(iX + ((iWidth / 14) * 9)), CInt(iY + ((iHeight / 10) * 8)), CInt(iX + iWidth), CInt(iY + ((iHeight / 10) * 9)))
		'- ears
		Dim trianglePoints(2) As Point
		trianglePoints(0) = New Point(CInt(iX + ((iWidth / 14) * 2)), CInt(iY))
		trianglePoints(1) = New Point(CInt(iX + ((iWidth / 14) * 3)), CInt(iY + ((iHeight / 10) * 6)))
		trianglePoints(2) = New Point(CInt(iX + ((iWidth / 14) * 6)), CInt(iY + ((iHeight / 10) * 3)))
		pictureBox.FillPolygon(New SolidBrush(Color.Orange), trianglePoints)
		trianglePoints(0) = New Point(CInt(iX + ((iWidth / 14) * 12)), CInt(iY))
		trianglePoints(1) = New Point(CInt(iX + ((iWidth / 14) * 11)), CInt(iY + ((iHeight / 10) * 6)))
		trianglePoints(2) = New Point(CInt(iX + ((iWidth / 14) * 8)), CInt(iY + ((iHeight / 10) * 3)))
		pictureBox.FillPolygon(New SolidBrush(Color.Orange), trianglePoints)
		'- done drawing cat!
	End Sub
End Class

2 answers to this question

Recommended Posts

  • 0
Dim bmp as New Bitmap(imgPictureBox.Width, imgPictureBox.Height, PixelFormat.Format32bppArgb)
Dim g as Graphics = Graphics.FromImage(bmp)

' Do drawing code here, but instead of pictureBox.DrawLine, do g.DrawLine

g.Dispose
imgPictureBox.Image = bmp
bmp.Dispose

  • 0

thanks jimbo, sorry for the late reply, been busy. i've used your code and ended up with the following. it successfully displays an image on load, and retains the image when you bring other applications into focus and then go back to it :), however, theres a bug in my code - when you click the submit button it crashes with an invalid parameter error on line 8 in the drawCat function! can you (or anyone) see the problem? thanks

Public Class frmDrawCat
	Private pictureBox As Graphics

	Private Sub frmDrawCat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'assign graphics variable to image box
		imgPicBox.Image = New Bitmap(imgPicBox.Width, imgPicBox.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
		pictureBox = Graphics.FromImage(imgPicBox.Image)

		'draw the initial cat
		Dim iValues() As Integer = {20, 20, 140, 100}
		drawCat(iValues)

		'cleanup memory
		pictureBox.Dispose()

		'setup text boxes with default values used above
		txtX.Text = iValues(0)
		txtY.Text = iValues(1)
		txtWidth.Text = iValues(2)
		txtHeight.Text = iValues(3)
	End Sub

	Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
		'capture input (co-ordinates, and dimentions)
		Dim iValues() As Integer = {Integer.Parse(txtX.Text), Integer.Parse(txtY.Text), Integer.Parse(txtWidth.Text), Integer.Parse(txtHeight.Text)}

		'draw the cat
		drawCat(iValues)
	End Sub

	Private Sub drawCat(ByVal iValues() As Integer)
		'get co-ordinates & sizes
		Dim iX As Integer = iValues(0)
		Dim iY As Integer = iValues(1)
		Dim iWidth As Integer = iValues(2)
		Dim iHeight As Integer = iValues(3)

		'clear graphics area
		pictureBox.Clear(Color.White)

		'create pens for drawing
		Dim blackPenW1 As New Pen(Color.Black, 1)
		Dim blackPenW2 As New Pen(Color.Black, 2)
		Dim blackPenW3 As New Pen(Color.Black, 3)
		Dim blackPenSolid As New SolidBrush(Color.Black)

		'draw cat graphics objects
		'- head
		pictureBox.FillEllipse(New SolidBrush(Color.Orange), CInt(iX + ((iWidth / 14) * 3)), CInt(iY + ((iHeight / 10) * 2)), CInt((iWidth / 14) * 8), CInt((iHeight / 10) * 8))
		'- eyes
		pictureBox.FillEllipse(blackPenSolid, CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 5)), CInt((iWidth / 14)), CInt((iHeight / 10)))
		pictureBox.FillEllipse(blackPenSolid, CInt(iX + ((iWidth / 14) * 8)), CInt(iY + ((iHeight / 10) * 5)), CInt((iWidth / 14)), CInt((iHeight / 10)))
		'- nose
		pictureBox.DrawLine(blackPenW2, CInt(iX + (iWidth / 2)), CInt(iY + ((iHeight / 10) * 6)), CInt(iX + (iWidth / 2)), CInt(iY + ((iHeight / 10) * 8)))
		Try
			pictureBox.DrawArc(blackPenW2, CInt(iX + ((iWidth / 14) * 6)), CInt(iY + ((iHeight / 10) * 7)), CInt(((iWidth / 14) * 1)), CInt(((iHeight / 10) * 1)), 0, 180)
			pictureBox.DrawArc(blackPenW2, CInt(iX + ((iWidth / 14) * 7)), CInt(iY + ((iHeight / 10) * 7)), CInt((iWidth / 14) * 1), CInt((iHeight / 10) * 1), 0, 180)
		Catch ex As Exception
		End Try
		'- wiskers
		pictureBox.DrawLine(blackPenW3, CInt(iX), CInt(iY + ((iHeight / 10) * 6)), CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 7)))
		pictureBox.DrawLine(blackPenW3, CInt(iX), CInt(iY + ((iHeight / 10) * 9)), CInt(iX + ((iWidth / 14) * 5)), CInt(iY + ((iHeight / 10) * 8)))
		pictureBox.DrawLine(blackPenW3, CInt(iX + ((iWidth / 14) * 9)), CInt(iY + ((iHeight / 10) * 7)), CInt(iX + iWidth), CInt(iY + ((iHeight / 10) * 6)))
		pictureBox.DrawLine(blackPenW3, CInt(iX + ((iWidth / 14) * 9)), CInt(iY + ((iHeight / 10) * 8)), CInt(iX + iWidth), CInt(iY + ((iHeight / 10) * 9)))
		'- ears
		Dim trianglePoints(2) As Point
		trianglePoints(0) = New Point(CInt(iX + ((iWidth / 14) * 2)), CInt(iY))
		trianglePoints(1) = New Point(CInt(iX + ((iWidth / 14) * 3)), CInt(iY + ((iHeight / 10) * 6)))
		trianglePoints(2) = New Point(CInt(iX + ((iWidth / 14) * 6)), CInt(iY + ((iHeight / 10) * 3)))
		pictureBox.FillPolygon(New SolidBrush(Color.Orange), trianglePoints)
		trianglePoints(0) = New Point(CInt(iX + ((iWidth / 14) * 12)), CInt(iY))
		trianglePoints(1) = New Point(CInt(iX + ((iWidth / 14) * 11)), CInt(iY + ((iHeight / 10) * 6)))
		trianglePoints(2) = New Point(CInt(iX + ((iWidth / 14) * 8)), CInt(iY + ((iHeight / 10) * 3)))
		pictureBox.FillPolygon(New SolidBrush(Color.Orange), trianglePoints)
		'- done drawing cat!
	End Sub
End Class

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.