Search This Blog

Tuesday, June 30, 2009

Annotating Images in C#

C# Snippet Tutorial - How to Draw Text on an Image

Example:

Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");

Now that we have a Bitmap object we can get a Graphics object from it. The Graphics object will be doing all of our text drawing.

Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");
Graphics g = Graphics.FromImage(myBitmap);

We can now use this Graphics object to perform all of our drawing. Let's start by simply placing some text in the upper left corner.

g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));


My sample code:
PictureBox image = sender as PictureBox;
Graphics g = Graphics.FromImage(image.Image as Bitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("1", new Font("Tahoma", 50), Brushes.Yellow, new PointF(0,0);



No comments: