Introduction
This article explains how to write text on an image using ASP.NET and C#. In this article I am using some common library functions and methods.
Image Class and Bitmap Class
This article explains how to write text on an image using ASP.NET and C#. In this article I am using some common library functions and methods.
Image Class and Bitmap Class
- The Image class is an example of an Abstract Class.
- The Bitmap class is an implementation of the Image class and it inherits from the Abstract Image class.
- After implementation we can get the Image Class functionality in the Bitmap Class.
C# Code
The following code is to write a text on an image. We can change the functionalities based on our requirements.
The following code is to write a text on an image. We can change the functionalities based on our requirements.
- //creating a image object
- System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image
- //draw the image object using a Graphics object
- Graphics graphicsImage = Graphics.FromImage(bitmap);
- //Set the alignment based on the coordinates
- StringFormat stringformat = new StringFormat();
- stringformat.Alignment = StringAlignment.Far;
- stringformat.LineAlignment = StringAlignment.Far;
- StringFormat stringformat2 = new StringFormat();
- stringformat2.Alignment = StringAlignment.Center;
- stringformat2.LineAlignment = StringAlignment.Center;
- //Set the font color/format/size etc..
- Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding
- Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding
- string Str_TextOnImage = "Happy";//Your Text On Image
- string Str_TextOnImage2 = "Onam";//Your Text On Image
- graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
- FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),
- stringformat); Response.ContentType = "image/jpeg";
- graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,
- FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),
- stringformat2); Response.ContentType = "image/jpeg";
- bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
Graphics & Bitmap
A Bitmap is an object used to work with images defined by pixel data and you can draw the image object using a Graphics object. Here's the code:
A Bitmap is an object used to work with images defined by pixel data and you can draw the image object using a Graphics object. Here's the code:
- //creating a image object
- System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image
- //draw the image object using a Graphics object
- Graphics graphicsImage = Graphics.FromImage(bitmap);
Formatting the string
Change Text Alignment based on the coordinates:
Change Text Alignment based on the coordinates:
- //Set the alignment based on the coordinates
- StringFormat stringformat = new StringFormat();
- stringformat.Alignment = StringAlignment.Far;
- stringformat.LineAlignment = StringAlignment.Far;
- StringFormat stringformat2 = new StringFormat();
- stringformat2.Alignment = StringAlignment.Center;
- stringformat2.LineAlignment = StringAlignment.Center;
Text On Image
Set the font color, size, format.
Set the font color, size, format.
- string Str_TextOnImage = "Happy";//Your Text On Image
- string Str_TextOnImage2 = "Onam";//Your Text On Image
- graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
- FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),
- stringformat); Response.ContentType = "image/jpeg";
- graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,
- FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),
- stringformat2); Response.ContentType = "image/jpeg";
String Color
Adding the string color in the following two ways.
Adding the string color in the following two ways.
- Color StringColor = System.Drawing.Color.Red;//direct color adding
- Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//customise color adding
Important Section
Namespace
Namespace
- using System.Drawing;
- using System.Drawing.Imaging;
The preceding namespace contains Graphics, Bitmaps, Image Editing and Alignment libraries and other libraries.
Output
Output

Reference
Summary
We learned how to write text on an image using ASP.NET and C#. I hope this article is useful for everyone.


Dev ChauhanPosted Jun 5, 2020, 2:07 AM EDT
How to Bind Name From DataBase
Sarathlal SaseendranPosted Dec 24, 2018, 8:46 PM EST
Great article Rajeesh Menoth. Thanks for sharing
hung thanhPosted Mar 12, 2017, 9:54 AM EDT
Image not save. Could you show me
Rajeesh MenothPosted Aug 30, 2016, 12:29 AM EDT
Thank You Delpin Susai Raj
Delpin Susai RajTOP 100Posted Aug 28, 2016, 3:15 AM EDT
Nice
Rajeesh MenothPosted Aug 25, 2015, 8:15 PM EDT
Thanks Pankaj Kumar Choudhary bro :)
Pankaj Kumar ChoudharyTOP 100Posted Aug 25, 2015, 5:28 PM EDT
Short but more informative article.......
Rajeesh MenothPosted Aug 25, 2015, 6:50 AM EDT
Thank you Shridhar Sharma :)
Shridhar SharmaPosted Aug 25, 2015, 6:32 AM EDT
good one :)
Rajeesh MenothPosted Aug 25, 2015, 6:03 AM EDT
Thanks Gopi Chand
Rajeesh MenothPosted Aug 25, 2015, 6:03 AM EDT
ok Rohit Nair
Gopi ChandPosted Aug 25, 2015, 5:57 AM EDT
Nice work
Rohit NairPosted Aug 25, 2015, 5:41 AM EDT
Rajeesh: Its Nair Rohit Subhash!!! its not the one u thinking..!
Rajeesh MenothPosted Aug 25, 2015, 5:38 AM EDT
Thanks Rakesh Chavda
RakeshPosted Aug 25, 2015, 5:36 AM EDT
nice one buddy
Rajeesh MenothPosted Aug 25, 2015, 5:29 AM EDT
Thanks Man Rohit Nair..Wish you a happy onam..!!! Nair Rohit sudakar ???
Rohit NairPosted Aug 25, 2015, 5:09 AM EDT
Nice article dude..!! Wish you a happy onam!!
Rajeesh MenothPosted Aug 25, 2015, 2:58 AM EDT
Thank you Rahul Prajapat
Rahul PrajapatPosted Aug 25, 2015, 2:48 AM EDT
Nice explain sir
Rajeesh MenothPosted Aug 25, 2015, 2:27 AM EDT
Thanks Ankit Bansal
Ankit BansalPosted Aug 25, 2015, 2:17 AM EDT
good..
Rajeesh MenothPosted Aug 25, 2015, 1:47 AM EDT
Thanks Mohammed Ibrahim
Rajeesh MenothPosted Aug 25, 2015, 1:47 AM EDT
Thanks Kalyankumar Ravichandran
Mohammed IbrahimPosted Aug 25, 2015, 1:40 AM EDT
nice work
Kalyankumar RavichandranPosted Aug 25, 2015, 12:53 AM EDT
Hi Rajeesh Good show man!
Rajeesh MenothPosted Aug 25, 2015, 12:30 AM EDT
Wish u the same Vaikesh K P..Happy Onam enjoy :)
Rajeesh MenothPosted Aug 25, 2015, 12:30 AM EDT
Thanks Vaikesh K P
Rajeesh MenothPosted Aug 25, 2015, 12:29 AM EDT
Thanks Karthikeyan K
Karthikeyan KPosted Aug 25, 2015, 12:29 AM EDT
Good one sir...Thanks for sharing
Vaikesh K PPosted Aug 25, 2015, 12:28 AM EDT
Nice Article :)
Vaikesh K PPosted Aug 25, 2015, 12:28 AM EDT
Happy Onam Rajeesh Menoth :)
Rajeesh MenothPosted Aug 25, 2015, 12:16 AM EDT
Thank you Gowtham K
Rajeesh MenothPosted Aug 25, 2015, 12:16 AM EDT
Afzaal Ahmad Zeeshan sir here i am using this code in web form..
Afzaal Ahmad ZeeshanTOP 100Posted Aug 24, 2015, 11:57 PM EDT
That is not ASP.NET, that is Windows Forms.
Gowtham KTOP 100Posted Aug 24, 2015, 11:52 PM EDT
Good one
Rajeesh MenothPosted Aug 24, 2015, 11:15 PM EDT
Thanks bro Upendra Pratap Shahi :)
Upendra Pratap ShahiPosted Aug 24, 2015, 11:09 PM EDT
nice share Rajeesh Menoth