Click here to Skip to main content
13,150,471 members (40,905 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 17 Jun 2004

Dynamically Write Text On An Image

, 17 Jun 2004
Rate this:
Please Sign up or sign in to vote.
If you have a pre-existing graphic and you want to write some text over it...


This article shows you how to dynamically write text on a pre-existing image, with ASP.NET and C#.

In my example, I have a picture of my son and I write the words “That’s my boy!”. Also, to show that you can combine graphic drawing and text together, I then draw an oval shape around the word that I just put on the picture.

In order to make this sample work, you need the following references:

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

Also, you might need to play around with where you want your text and oval to go. Remember that both the placement of the text and graphic oval are based on X and Y coordinates. So, if you have to move the text/oval up, then change the value of the Y parameter. If you need to move the text/oval horizontally, then change the value of the X parameter.

There are basically 6 steps for my sample:

  1. Load your image:
    //Load the Image to be written on.
    Bitmap bitMapImage = new 
       System.Drawing.Bitmap(Server.MapPath("dallen.jpg" )  );
    Graphics graphicImage = Graphics.FromImage( bitMapImage ); 
  2. Set the graphics to be smooth.
    //Smooth graphics is nice.
    graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
  3. Write your text. Here is where you set your font. The X and Y coordinates are in the new point. (100 = X, 250 = Y).
    //Write your text.
    graphicImage.DrawString( "That's my boy!", 
       new Font("Arial", 12,FontStyle.Bold ), 
       SystemBrushes.WindowText, new Point( 100, 250 ) ); 
  4. Draw your oval around the text. Note: play around with the numbers to make the oval to the correct size that you want.
    //I am drawing a oval around my text.
    graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360); 
  5. Set the Content Type to jpg and then write your image to the response stream.
    //Set the content type
    //Save the new image to the response output stream.
    bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
  6. Clean up.
    //Clean house.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer DolphLarson.Com
United States United States
Dolph Larson is a Microsoft Certified Solution Developer (MCSD) for .Net, Microsoft Certified Application Developer (MCAD) and Microsoft Certified Database Administrator (MCDBA). He has over fourteen years of professional combined programming, database administration and information technology experience. He currently works as a senior developer for Omnicell and has worked as a senior web developer for Dolph also managed the programming department for Health Help, Inc. and has seven years of managed health care experience and three years of energy experience.

Right now, I am developing Compact Framework applications for the PocketPC and Winodws Mobile. Go TO for FREE Pocket PC software!!!!!

You may also be interested in...


Comments and Discussions

GeneralSomeone copied your article Pin
lols12-Oct-08 23:27
memberlols12-Oct-08 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 18 Jun 2004
Article Copyright 2004 by Dolph
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid