In this article I will explain how to create text image on the fly with ASP.Net using C# and VB.Net.
		The image generation is done dynamically using GDI+ library in ASP.Net.
	
		 
	
		 
		HTML Markup
	
		Below is the HTML markup of the page where I have a TextBox where the text to be converted will be entered, a Button control to trigger the conversion process and an Image Control to display the converted image.
	
		
			<html xmlns="http://www.w3.org/1999/xhtml">
		
			<head runat="server">
		
			    <title></title>
		
			</head>
		
			<body>
		
			    <form id="form1" runat="server">
		
			    <asp:TextBox runat="server" ID="txtText"></asp:TextBox>
		
			    <asp:Button ID="btnConvert" runat="server" Text="Convert" OnClick="btnConvert_Click" />
		
			    <hr />
		
			    <asp:Image ID="imgText" runat="server" Visible="false" />
		
			    </form>
		
			</body>
		
			</html>
	 
	
		 
	
		 
	
		Namespaces
	
		You will need to inherit the following namespaces
	
		C#
	
		
			using System.Drawing.Text;
		
			using System.Drawing;
		
			using System.Drawing.Drawing2D;
		
			using System.IO;
		
			using System.Drawing.Imaging;
	 
	
		 
	
		VB.Net
	
		
			Imports System.Drawing.Text
		
			Imports System.Drawing
		
			Imports System.Drawing.Drawing2D
		
			Imports System.IO
		
			Imports System.Drawing.Imaging
	 
	
		 
	
		 
	
		Text String to Image Conversion
	
		The below code converts the text string to a JPEG Image file. The image file that is generated is saved on the disk and then the image is displayed in Image Control.
	
		C#
	
		
			protected void btnConvert_Click(object sender, EventArgs e)
		
			{
		
			    string text = txtText.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();
		
			    string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
		
			    bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
		
			    imgText.ImageUrl = "~/images/" + fileName;
		
			    imgText.Visible = true;
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub btnConvert_Click(sender As Object, e As System.EventArgs)
		
			    Dim text As String = txtText.Text.Trim()
		
			    Dim bitmap As New Bitmap(1, 1)
		
			    Dim font As New Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel)
		
			    Dim graphics As Graphics = graphics.FromImage(bitmap)
		
			    Dim width As Integer = CInt(graphics.MeasureString(text, font).Width)
		
			    Dim height As Integer = CInt(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()
		
			    Dim fileName As String = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) & ".jpg"
		
			    bitmap.Save(Server.MapPath("~/images/") & fileName, ImageFormat.Jpeg)
		
			    imgText.ImageUrl = "~/images/" & fileName
		
			    imgText.Visible = True
		
			End Sub
	 
	
		 
	
		 
	
		Screenshots
	
	
		 
	
		Demo
	
	
		 
	
		 
	
		Downloads