Wednesday, August 5, 2009

Adding Text to Images

Adding text to an image requires 2 steps:
  • Create a Graphics object
  • Create a Font object
  • Call Graphics.DrawString method
Code Example:
private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Font f = new Font("Arial", 12, FontStyle.Bold);
    g.DrawString("Copyright 2009", f, Brushes.Black, 10, 10);
}
Why would you do this?
Most likely you wouldn't. You would use a label instead. But you can also draw strings on images and that is most likely what you would use this method for.

More Info: MSDN: Graphics.DrawString Method