[RESOLVED] [2005] Graphics.DrawString
Hello everyone!
I'm declaring my Graphics object like this :
Code:
Private bCircle As New Bitmap(700, 700)
'Declare The Graphics Object To Draw Onto
Private gCircle As Graphics = Graphics.FromImage(bCircle)
Private sPicText(7) As String
Private iPicIndex As Integer
As you can see, I declared an array named sPicText with 8 elements, and also declared an integer named iPicIndex this will increment, based on a button being clicked, or decrement based on another button being clicked.
I initialise my array like this :
Code:
Private Sub frmPreview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
iPicIndex = 0
sPicText(0) = "Picture 1 ( One )"
sPicText(1) = "Picture 2 ( Two )"
sPicText(2) = "Picture 3 ( Three )"
sPicText(3) = "Picture 4 ( Four )"
sPicText(4) = "Picture 6 ( Six )"
sPicText(5) = "Picture 1 ( Seven )"
sPicText(6) = "Picture 8 ( Eight )"
sPicText(7) = "Picture 9 ( Nine )"
gCircle.DrawString(sPicText(iPicIndex).ToString, New System.Drawing.Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.AliceBlue, pic5.Left, pic5.Top)
End Sub
Now, I want to use Graphics.DrawString to draw the particular array, based on the index. When the form is loaded, it does show the contents of sPicText(0), which is "Picture 1 ( One )".
When I click the next button, it is supposed to update this string that is being drawn. Same With the Previous button. Here's the code :
Code:
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
iPicIndex += 1
If iPicIndex > 7 Then iPicIndex = 0
gCircle.DrawString(sPicText(iPicIndex).ToString, New System.Drawing.Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.AliceBlue, pic5.Left, pic5.Top)
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
iPicIndex -= 1
If iPicIndex < 0 Then iPicIndex = 7
gCircle.DrawString(sPicText(iPicIndex).ToString, New System.Drawing.Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.AliceBlue, pic5.Left, pic5.Top)
End Sub
Even when clicking either of these buttons umpteen times, the string doesn't get updated. It remains with "Picture 1 ( One )", IOW. sPicText(0)
How can I update this ?
Re: [2005] Graphics.DrawString
Another soliloquy :)
OK, all I needed to this :
Code:
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
iPicIndex -= 1
If iPicIndex < 0 Then iPicIndex = 7
gCircle.DrawString(sPicText(iPicIndex).ToString, New System.Drawing.Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.AliceBlue, pic5.Left, pic5.Top, New System.Drawing.StringFormat(StringFormatFlags.FitBlackBox))
picDraw.Image = bCircle
End Sub
That at least, updated the string, but the problem was that it drew over the already existing text. All I did to fix that was to clear the graphic :
Code:
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
iPicIndex -= 1
If iPicIndex < 0 Then iPicIndex = 7
gCircle.Clear(picDraw.BackColor)
gCircle.DrawString(sPicText(iPicIndex).ToString, New System.Drawing.Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.AliceBlue, pic5.Left, pic5.Top, New System.Drawing.StringFormat(StringFormatFlags.FitBlackBox))
picDraw.Image = bCircle
End Sub
Thanx anyways :D
Re: [RESOLVED] [2005] Graphics.DrawString
You shouldnt draw on the form like that. Always use the forms paint event.
Try minimizing the form and bring it back up and you'll see the negative effects of not using the paint event.
Re: [RESOLVED] [2005] Graphics.DrawString
Thanx for the advice Atheist, but I'm drawing on a Picturebox, named picDraw. When I minimise my form which contains this picturebox, and restore it again, everything is still displayed as it was before minimising
Re: [RESOLVED] [2005] Graphics.DrawString
You're not drawing on a PictureBox. You're drawing on an Image. The two are not the same thing. A PictureBox displays an Image but an Image can exist without a PictureBox. If you draw on a PictureBox then it disappears the next time the control is repainted, as with all controls. If you draw on an Image then it is permanent, which means it cannot be erased. That's why you were drawing on top of the existing text originally. Calling the Clear method of the Graphics object is OK in this situation but if there was something else on the Image you didn't want lost then you'd be stuffed.
Re: [RESOLVED] [2005] Graphics.DrawString
Quote:
Originally Posted by jmcilhinney
You're not drawing on a PictureBox. You're drawing on an Image. The two are not the same thing.
I know ;) Sorry for the wrong phrasing :)
Quote:
Originally Posted by jmcilhinney
If you draw on an Image then it is permanent, which means it cannot be erased. That's why you were drawing on top of the existing text originally. Calling the Clear method of the Graphics object is OK in this situation but if there was something else on the Image you didn't want lost then you'd be stuffed.
Yeah, I had to redraw something small again, because of this, luckily it wasn't something hefty, so it doesn't take up too much processing :)
Can there be a different way of doing this then, or should I rather stick to what I have, and what is working as I intended ?
Re: [RESOLVED] [2005] Graphics.DrawString
If you want to draw permanently on an Image then that's what you should do. Otherwise you should do as suggested and draw on a control in its Paint event handler. It's very simple. You just store the data that controls what gets drawn in member variables. In the Paint event handler you read those variables and do the drawing. That way your drawing gets recreated every time the control is redrawn. If you want to change the drawing you simply change the data in the variables and force the control to repaint. Check out this thread for a simple example of temporary drawing on a control an permanent drawing on an Image.