Click here to Skip to main content
13,300,002 members (70,084 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 28 Dec 2006

ImageLabel Control for ASP.NET (Potential CAPTCHA Control)

, 13 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Labels are generated as images instead of text to have more privacy without any configurations and without HttpHandlers.

Sample Image - ImageLabel.png


This an ASP.NET web control for generating labels as images. You might want to do that for providing security for e-mail crawlers or to disallow copy paste of the text. Many similar controls have been implemented as HttpHandlers, so you need more configuration to make them work. However, this control does not need any configuration. You just need to use it just like any other ASP.NET control like Label. Almost all of the label formatting options are included, like font size, backcolor, and forecolor.

How does it work

The code can be separated to two parts: the variable initializations, and the render part. The OnInit event is used for the preparation of a URL string of the image to be generated. The URL string is stored in a private member of the class with the client to make sure that controls draw themselves. There is a trick here. This control is executed twice by the browser. To make things work, the query string is in a predefined format. The browser tries to display the image specified by a tag, and the init code tries to read whether it is coming from a querystring or not to generate the image.

protected override void OnInit(EventArgs e)
    if (this.Context.Request.QueryString[QueryStringText()] != null)
    this.m_URL = Context.Request.Url.AbsolutePath + "?" + 
                    secretQueryString+ this.ClientID + "=1";

However, there is another problem. Since the browser is executing twice, the control state is also different, so storing the display text value will not work using the ViewState. As a result, I store it in the session variable, that is cleared after rendering the image. The Text property of the control handles the session and the automatic code generated by the designer. You know, when you set the text of the control from the designer, after compilation, it is executed by the ASP.NET process. I'm telling this because this control runs two controls with different life cycles. "this.Context.Request.QueryString[QueryStringText()]" makes sure that we are coming from the main page, rather than the forked control.

public string Text
        if (DesignMode)
            if (ViewState["Text"] == null)
                ViewState["Text"] = string.Empty;
            return (string)ViewState["Text"];
            string s = PossiblequeryString();

            if (!string.IsNullOrEmpty(s))
                return (string)Context.Session[s]; 

            if (Context.Session[SessionText()] == null)
                Context.Session[SessionText()] = string.Empty;
            return (string)Context.Session[SessionText()];

        if (DesignMode)
            ViewState["Text"] = value;

            if (PossiblequeryString() == null)
                Context.Session[SessionText()] = value;

The DrawImage function draws the image in memory and saves to the Response object. Since the Response.End method is executed, Render events will not be fired any more at image generation.

strOutput.DrawString(Message, msgFont, new SolidBrush(foreColor), 1, 1);
MemoryStream memStream = new MemoryStream();
bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

Context.Response.ContentType = "image/png";

The Render code is used for the rendering of the image. It is simply an image HTML tag pointing to the URL generated previously on onInit. To make the Visual Studio Designer happy and stop it complaining about exceptions, I also added code to render itself as a normal text control in the designer.

protected override void Render(HtmlTextWriter writer)
    if (DesignMode)
        writer.AddAttribute(HtmlTextWriterAttribute.Src, this.m_URL);


This control works without any configuration, and is easy to use as any other ASP.NET Web Control. It can be used to hide information from crawlers or robots. Although this is not implemented as a CAPTCHA control, that functionality can easily be added to the image generation.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Can Erten
Software Developer
Turkey Turkey

You may also be interested in...


Comments and Discussions

GeneralRuntime Settings Pin
Brad Bruce29-Dec-06 10:46
memberBrad Bruce29-Dec-06 10:46 
AnswerRe: Runtime Settings Pin
Can Erten29-Dec-06 14:30
memberCan Erten29-Dec-06 14:30 
GeneralRe: Runtime Settings Pin
Brad Bruce29-Dec-06 15:08
memberBrad Bruce29-Dec-06 15:08 
AnswerRe: Runtime Settings Pin
Can Erten29-Dec-06 15:28
memberCan Erten29-Dec-06 15:28 
GeneralRe: Runtime Settings Pin
Brad Bruce30-Dec-06 8:30
memberBrad Bruce30-Dec-06 8:30 
AnswerRe: Runtime Settings Pin
Can Erten13-Jan-07 14:46
memberCan Erten13-Jan-07 14:46 
GeneralDown with HttpHandlers Pin
Brad Bruce29-Dec-06 6:22
memberBrad Bruce29-Dec-06 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 13 Jan 2007
Article Copyright 2006 by Can Erten
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid