In this article I am going to show how we can generate
image of any text.
Below is my aspx code:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Generate Image of Text</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<table
style="width: 90%; background-color: #F5F5F5;
border: solid 10px Green;" align="center"
cellpadding="10" cellspacing="10">
<tr>
<td height="10px" style="background-color:
Yellow; font-weight:
bold; font-family:
Georgia;
font-size:
18pt; color: Red;" align="center">
Generate Image of Your Text
</td>
</tr>
<tr>
<td>
Enter Text #:
<asp:TextBox runat="server" ID="txtYourText" Width="300px"></asp:TextBox>
<asp:Button ID="btnConvert" runat="server" Text="Generate Image" OnClick=" btnGenerateImage _Click"
/>
</td>
</tr>
<tr>
<td>
<asp:Image ID="imgText" runat="server" Visible="false" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Below is my aspx.cs code:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
public partial
class _Default
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
btnGenerateImage_Click(object sender, EventArgs e)
{
string text = txtYourText.Text.Trim();
Bitmap bitmap = new Bitmap(1, 1);
Font font = new
Font("Arial",
25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text,
font).Width;
int height = (int)graphics.MeasureString(text,
font).Height;
bitmap = new Bitmap(bitmap,
new Size(width,
height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255,
0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
Random random = new Random();
string fileName = Convert.ToString(random.Next(1, 2000)) + ".jpg";
bitmap.Save(Server.MapPath("~/MyTextImages/")
+ fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "~/MyTextImages/"
+ fileName;
imgText.Visible = true;
}
}
Now run the application:

Image 1.

Image 2.
Now you can view your all images..

Image 3.