Q

Putting text into an image control

How do I put data (text) into an image control?
If you mean the System.Windows.Forms.PictureBox class for Windows Forms applications, it's as easy as overriding the Paint event for the control and invoking the DrawString method on the PaintEventArgs Graphics member. For example:
       private void pictureBox1_Paint(object sender, 
System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawString("Foo", 
new System.Drawing.Font("Courier", 12), 
System.Drawing.Brushes.Black, 
(float)0.0, (float)0.0);
        }
If you mean the System.Web.UI.WebControls.Image class for Web Forms applications, the only thing you can really do is specify an alternate text string and not set a valid URL for the ImageUrl property. Of course, you can always create an image file on the fly using the .NET drawing routines and then render that dynamically generated image file by specifying it as the ImageUrl property of the Image control. Jeff Prosise's Wicked Code column in MSDN Magazine August 2002 issues shows how to do this.
This was first published in May 2004

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close