DotNetLines Blogs

DotNetLines Blog

Convert text string to image like CAPTCHA in ASP.Net using C#

Sep 24

Written by:
9/24/2013 10:14 PM  RssIcon

Many applications use a technique which we call it as CAPTCHA. These captcha are very useful in knowing whether a user is really a human being or not.  In this post we are going to see how to convert a text to image in ASP.NET using C#. If you extend it further then you can develop your own captcha application.

We have already seen some of the posts like Create a simple Pie Chart in ASP.NET using C#, Create a simple Column Chart / Bar Chart in ASP.NET using C#, Re-size an image by keeping its aspect ratio in jQuery, Re-size an image by maintaining its aspect ratio, Avoid Skype toolbar to Phone/Fax number.

To convert a text to image let us have a Textbox control.  User will enter text in this Textbox . After clicking on Convert button the Text will be converted to image.

Let us have the following form on the .aspx page or .ascx control.

    <form id="formtextToImage" runat="server">



                Convert text to Image</h3>


        <asp:Label ID="lblText" runat="server" Text="Enter your text to convert it to image"></asp:Label>

        <asp:TextBox runat="server" ID="txtText"></asp:TextBox>

        <asp:Button ID="btnTextToImage" runat="server" Text="Convert Text to Image" OnClick="btnTextToImage_Click" />

        <br />

        <asp:Image ID="imgTextImage" runat="server" Visible="false" />


We will have to use following Namespaces also at the top in code-behind.

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

using System.Drawing.Text;

using System.IO;


After clicking on button btnTextToImage write the following code in code-behind.

protected void btnTextToImage_Click(object sender, EventArgs e)


    string text = txtText.Text.Trim();


    Bitmap bmp = new Bitmap(1, 1);


    //Set the font style of output image

    Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);


    Graphics graphics = Graphics.FromImage(bmp);


    int width = (int)graphics.MeasureString(text, font).Width;

    int height = (int)graphics.MeasureString(text, font).Height;


    bmp = new Bitmap(bmp, new Size(width, height));


    graphics = Graphics.FromImage(bmp);


    //Specify the background color of the image


    graphics.SmoothingMode = SmoothingMode.AntiAlias;

    graphics.TextRenderingHint = TextRenderingHint.AntiAlias;


    //Specify the text, font, Text Color, X position and Y position of the image

    graphics.DrawString(text, font, new SolidBrush(Color.Blue), 0, 0);





    //The image will be saved with following name

    string fileName = "text.jpg";


    //Save the Bitmap in jpg format

    bmp.Save(Server.MapPath("/images/") + fileName, ImageFormat.Jpeg);

    imgTextImage.ImageUrl = "/images/" + fileName;


    imgTextImage.Visible = true;



Here we are converting the text to image using graphics.DrawString method. We are saving it with a specific name in images folder. Then we are fetching it again from that location and displaying it using imgTextImage control.

Copyright ©2013

Tags: Image ASP.Net C#
Location: Blogs Parent Separator ASP.NET

1 comment(s) so far...


Re: Convert text string to image like CAPTCHA in ASP.Net using C#

Cool tips, thanks!

By on   10/6/2013 3:15 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 


You must be logged in and have permission to create or edit a blog.