Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DrawString not being displayed in a PictureBox

Status
Not open for further replies.

snuv

Programmer
Oct 30, 2001
751
GB
I am trying to draw labels over a graduated image so that image shows through the label rather than the form's background colour

For some reason, the text wont show up although the graduated image is being displayed

My code is

Code:
Friend WithEvents picBackground As System.Windows.Forms.PictureBox

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()
    'Add any initialization after the InitializeComponent() call
    Me.picBackground.Image = resource.Image
    DrawString("test",552,200)
End Sub

Private sub DrawString(ByVal str as String, ByVal x as Integer, ByVal y as Integer)
	Dim xgraphics As Graphics
	xgraphics = Me.picBackground.CreateGraphics
	Dim col as Color = Color.FromArgb(255, 255, 255)
	Dim f as Font = New Font("Microsoft Sans Serif", 8.25)
	dim b as Brush = New SolidBrush(col)
	xgraphics.DrawString(str, f, b,x,y)
	xgraphics.Flush
End Sub

Can anyone help?

Cheers
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
What is the image that is being displayed? Is it white? You are printing your text in white (Color.FromArgb(255, 255, 255)).



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
good thought but the image is dark blue

Cheers
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
I'll bet it has something to do with being called from sub new.

A way to validate this is to try putting your drawstring call in a button on the form, and then press the button to see if the text shows up.

If that's the case its just a matter of figuring out where to put your function call.
 
It's because on the constructor, the rendering is yet to occur. And once the system loads, or draws, the client window, only then your application will receive PAINT messages. Also, when an out-of-scope client window, or just parts of it, is "uncovered", you also receive a PAINT message. On .NET, this is equivalent to the Paint event being raised.

In short, you need to call drawString again from the Paint event. (It's interesting to know also that the Paint event args has a Graphics attribute so you don't need to create a new one)

Hope this helps. ;)
 
I tried adding a paint handler
Code:
Private shadows sub paint(ByVal sender as Object, ByVal e as Windows.Forms.PaintEventArgs) handles MyBase.Paint
    Dim col as Color = Color.FromArgb(255,255,255)
    Dim f as Font = New Font("Microsoft Sans Serif", 8.25)
    dim b as Brush = New SolidBrush(col)
    e.Graphics.DrawString("Test", f, b,552,200)
End Sub
but the string still doesnt appear

Any suggestions?

Thanks
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
It's because the background image is drawn in front of the graphics try not setting the image as background but drawing it to the graphics object. something like this

Code:
Friend WithEvents picBackground As System.Windows.Forms.PictureBox

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()
    'Add any initialization after the InitializeComponent() call
    DrawString("test",552,200)
End Sub

Private sub DrawString(ByVal str as String, ByVal x as Integer, ByVal y as Integer)
    Dim xgraphics As Graphics
    xgraphics = Me.picBackground.CreateGraphics
    Dim col as Color = Color.FromArgb(255, 255, 255)
    Dim f as Font = New Font("Microsoft Sans Serif", 8.25)
    dim b as Brush = New SolidBrush(col)
    xgraphics.drawimage(resource.image,0,0,0,0) 'other parameters to add
    xgraphics.DrawString(str, f, b,x,y)
    xgraphics.Flush
End Sub

Christiaan Baes
Belgium

"My new site" - Me
 
Thanks for all your help

In the end I took all the drawing out of the constructor,
called the new functions from the calling class and made the changes to the image before adding it to the picture box

Code:
Public Sub DrawImage()
    picBackground.Image = GetImage() 
End Sub


Private Function GetImage() as System.Drawing.Image
   Dim RetVal as New Bitmap(Resource.image)

   Dim xgraphics as Graphics = Graphics.FromImage(RetVal)
   Dim col as Color = Color.FromArgb(255,255,255)
   Dim f as Font = New Font("Microsoft Sans Serif", 8.25)
   dim b as Brush = New SolidBrush(col)

   xgraphics.DrawString("Test", f, b,552,200)
   xgraphics.Flush

   return RetVal
End Function


Cheers
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top