Write Text on Image Using ASP.NET and C#

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
  • 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.
  1. //creating a image object    
  2. System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image     
  3. //draw the image object using a Graphics object    
  4. Graphics graphicsImage = Graphics.FromImage(bitmap);    
  5.   
  6. //Set the alignment based on the coordinates       
  7. StringFormat stringformat = new StringFormat();    
  8. stringformat.Alignment = StringAlignment.Far;    
  9. stringformat.LineAlignment = StringAlignment.Far;    
  10.   
  11. StringFormat stringformat2 = new StringFormat();    
  12. stringformat2.Alignment = StringAlignment.Center;    
  13. stringformat2.LineAlignment = StringAlignment.Center;    
  14.   
  15. //Set the font color/format/size etc..      
  16. Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding    
  17. Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding    
  18. string Str_TextOnImage = "Happy";//Your Text On Image    
  19. string Str_TextOnImage2 = "Onam";//Your Text On Image    
  20.   
  21. graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,    
  22. FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),    
  23. stringformat); Response.ContentType = "image/jpeg";    
  24.   
  25. graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,    
  26. FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),    
  27. stringformat2); Response.ContentType = "image/jpeg";    
  28. 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: 
  1. //creating a image object    
  2. System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image     
  3. //draw the image object using a Graphics object    
  4. Graphics graphicsImage = Graphics.FromImage(bitmap);   
Formatting the string

Change Text Alignment based on the coordinates:
  1. //Set the alignment based on the coordinates       
  2. StringFormat stringformat = new StringFormat();    
  3. stringformat.Alignment = StringAlignment.Far;    
  4. stringformat.LineAlignment = StringAlignment.Far;    
  5.   
  6. StringFormat stringformat2 = new StringFormat();    
  7. stringformat2.Alignment = StringAlignment.Center;    
  8. stringformat2.LineAlignment = StringAlignment.Center;  
Text On Image

Set the font color, size, format.
  1. string Str_TextOnImage = "Happy";//Your Text On Image    
  2. string Str_TextOnImage2 = "Onam";//Your Text On Image    
  3.   
  4. graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,    
  5. FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),    
  6. stringformat); Response.ContentType = "image/jpeg";    
  7.   
  8. graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,    
  9. FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),    
  10. stringformat2); Response.ContentType = "image/jpeg";    
String Color

Adding the string color in the following two ways.
  1. Color StringColor = System.Drawing.Color.Red;//direct color adding    
  2. Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//customise color adding   
Important Section

Namespace
 
  1. using System.Drawing;  
  2. using System.Drawing.Imaging;   
The preceding namespace contains Graphics, Bitmaps, Image Editing and Alignment libraries and other libraries.

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.   


Similar Articles