Click here to Skip to main content
16,019,976 members
Articles / Web Development / ASP.NET
Article

Dynamically Write Text On An Image

Rate me:
Please Sign up or sign in to vote.
4.74/5 (36 votes)
17 Jun 20041 min read 315.1K   12.7K   73   29
If you have a pre-existing graphic and you want to write some text over it...

Introduction

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:

C#
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:
    C#
    //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.
    C#
    //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).
    C#
    //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.
    C#
    //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.
    C#
    //Set the content type
    Response.ContentType="image/jpeg"; 
    //Save the new image to the response output stream.
    bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
  6. Clean up.
    C#
    //Clean house.
    graphicImage.Dispose();
    bitMapImage.Dispose();

License

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


Written By
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 eRealty.com. 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 http://www.iPocketPC.net for FREE Pocket PC software!!!!!

Comments and Discussions

 
QuestionThe text written on image is long Pin
Xtrimist4-Dec-20 9:18
Xtrimist4-Dec-20 9:18 
Questionhow to display only image without background ? Pin
joelprabhu15-May-15 2:11
joelprabhu15-May-15 2:11 
GeneralMy vote of 5 Pin
Altaf N Patel8-May-13 1:54
Altaf N Patel8-May-13 1:54 
GeneralMy vote of 5 Pin
Ruwan Jayalath20-Apr-13 22:08
Ruwan Jayalath20-Apr-13 22:08 
QuestionText on an image using Visual c# Express Pin
Diwakar Prajapati6-Aug-12 8:04
Diwakar Prajapati6-Aug-12 8:04 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey5-Mar-12 19:33
professionalManoj Kumar Choubey5-Mar-12 19:33 
Generaluseful Pin
BillW3315-Apr-11 3:40
professionalBillW3315-Apr-11 3:40 
QuestionWorks in Visual Studio but NOT on server. Pin
bretBolton30-Apr-09 5:56
bretBolton30-Apr-09 5:56 
GeneralSomeone copied your article Pin
lols12-Oct-08 23:27
lols12-Oct-08 23:27 
GeneralThanks alot its vere useful Pin
romanr10016-Sep-08 0:26
romanr10016-Sep-08 0:26 
QuestionResponse.OutputStream & HTML Pin
bcalder125-Oct-07 1:02
bcalder125-Oct-07 1:02 
GeneralThis code saved me Pin
JLibertor20-Oct-07 5:29
JLibertor20-Oct-07 5:29 
QuestionHelp I' am news Pin
DataBrain16-Mar-07 5:09
DataBrain16-Mar-07 5:09 
GeneralI18N Pin
Nero197216-Jan-07 10:25
Nero197216-Jan-07 10:25 
QuestionHow to make image quality high ? Pin
hemper14-Sep-06 2:56
hemper14-Sep-06 2:56 
Question??? Pin
papa198016-May-06 5:31
papa198016-May-06 5:31 
AnswerRe: ??? Pin
JLibertor20-Oct-07 5:27
JLibertor20-Oct-07 5:27 
Generalmessage should come vertical Pin
Md.Serajuddin22-Mar-06 3:14
Md.Serajuddin22-Mar-06 3:14 
GeneralFont Color Pin
Anonymous24-Aug-05 4:37
Anonymous24-Aug-05 4:37 
GeneralRe: Font Color Pin
hocki30-Sep-05 3:12
hocki30-Sep-05 3:12 
GeneralRe: Font Color Pin
Member 162415011-Mar-08 3:04
Member 162415011-Mar-08 3:04 
GeneralDispose() goes in a finally block... Pin
Jason Shaver22-Jun-05 4:22
Jason Shaver22-Jun-05 4:22 
GeneralRe: Dispose() goes in a finally block... Pin
Daniel Turini22-Jun-05 4:37
Daniel Turini22-Jun-05 4:37 
GeneralRe: Dispose() goes in a finally block... Pin
Dolph Larson22-Jun-05 9:12
sussDolph Larson22-Jun-05 9:12 
GeneralPrinting Pin
Jeff Rush7-Feb-05 12:27
Jeff Rush7-Feb-05 12: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.