Generate Image of any Text

In this article I am going to show how we can generate image of any text.

Below is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Generate Image of Text</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <table style="width: 90%; background-color: #F5F5F5; border: solid 10px Green;" align="center"  
  12.             cellpadding="10" cellspacing="10">  
  13.             <tr>  
  14.                 <td height="10px" style="background-color: Yellow; font-weight: bold; font-family: Georgia;  
  15.                     font-size: 18pt; color: Red;" align="center">  
  16.                     Generate Image of Your Text  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td>  
  21.                     Enter Text #:  
  22.                     <asp:TextBox runat="server" ID="txtYourText" Width="300px"></asp:TextBox>  
  23.                     <asp:Button ID="btnConvert" runat="server" Text="Generate Image" OnClick="btnGenerateImage _Click" />  
  24.                 </td>  
  25.             </tr>  
  26.             <tr>  
  27.                 <td>  
  28.                     <asp:Image ID="imgText" runat="server" Visible="false" />  
  29.                 </td>  
  30.             </tr>  
  31.         </table>  
  32.     </div>  
  33.     </form>  
  34. </body>  
  35. </html>  
Below is my aspx.cs code:
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Drawing.Text;  
  13. using System.Drawing;  
  14. using System.Drawing.Drawing2D;  
  15. using System.IO;  
  16. using System.Drawing.Imaging;  
  17.   
  18. public partial class _Default : System.Web.UI.Page  
  19. {  
  20.     protected void Page_Load(object sender, EventArgs e)  
  21.     {  
  22.   
  23.     }  
  24.     protected void btnGenerateImage_Click(object sender, EventArgs e)  
  25.     {  
  26.         string text = txtYourText.Text.Trim();  
  27.         Bitmap bitmap = new Bitmap(1, 1);  
  28.         Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);  
  29.         Graphics graphics = Graphics.FromImage(bitmap);  
  30.         int width = (int)graphics.MeasureString(text, font).Width;  
  31.         int height = (int)graphics.MeasureString(text, font).Height;  
  32.         bitmap = new Bitmap(bitmap, new Size(width, height));  
  33.         graphics = Graphics.FromImage(bitmap);  
  34.         graphics.Clear(Color.White);  
  35.         graphics.SmoothingMode = SmoothingMode.AntiAlias;  
  36.         graphics.TextRenderingHint = TextRenderingHint.AntiAlias;  
  37.         graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);  
  38.         graphics.Flush();  
  39.         graphics.Dispose();  
  40.   
  41.         Random random = new Random();  
  42.         string fileName = Convert.ToString(random.Next(1, 2000)) + ".jpg";  
  43.   
  44.         bitmap.Save(Server.MapPath("~/MyTextImages/") + fileName, ImageFormat.Jpeg);  
  45.         imgText.ImageUrl = "~/MyTextImages/" + fileName;  
  46.         imgText.Visible = true;  
  47.     }  
  48. }  
Now run the application:

generate image of your text
                                                                           Image 1.

enter text
                                                                              Image 2.

Now you can view your all images:

coding output
                                                               Image 3.