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