Joel R
Joel R

Reputation: 79

text not showing on bitmap image in C#

I can't seem to get the text I've written to show up on my image Here's the code I'm using

//Creates a bitmap with the path to the current image
Bitmap LabelImage = new Bitmap(dtImages.Rows[intCurrentImage]["ImageURL"].ToString());

Graphics graphic = Graphics.FromImage(LabelImage);

graphic.DrawString("Hello", new Font("Tahoma",40), Brushes.Azure, new System.Drawing.Point(0,0));

//put Image that I just created and put the text on into an Infragistics UltraPicureBox
picImage.Image = LabelImage

Upvotes: 1

Views: 712

Answers (2)

Bala R
Bala R

Reputation: 109027

I just tried this

 Bitmap bitmap = new Bitmap("C:\\Untitled.png");
 Graphics g = Graphics.FromImage(bitmap);
 g.DrawString("Hello", new Font("Tahoma", 40), Brushes.Azure, new System.Drawing.Point(0, 0));
 pictureBox1.Image = bitmap;

and it works fine for me. Just try to pick a contrasting brush.

Upvotes: 0

Oded
Oded

Reputation: 499322

You did not update your original image (LabelImage), so why should the text you added to the Graphics object show up?.

From MSDN, Graphics.FromImage:

Creates a new Graphics from the specified Image.

(emphasis mine)

After you have added the text, you need to save the changes:

graphic.Save();

Unrelated to your question, you should really put the creation of the Graphics object in a using statement, to ensure proper disposal:

using(Graphics graphic = Graphics.FromImage(LabelImage))
{
   // use graphic here
}

Upvotes: 4

Related Questions