Sections
     
     
     
     
     
WWWCoder.com Resource Directory

Generating a Registration Image on the Fly
10/10/2006 11:20:35 AM

This article focuses on generating an image on the fly in ASP.NET. This feature you may find on any website that requires registration of a user. Example: Yahoo Registration, this site requires you to type in your secret string before you can successfully login.

This article focuses on generating an image on the fly in ASP.NET. This feature you may find on any website that requires registration of a user. Example: Yahoo Registration, this site requires you to type in your secret string before you can successfully login. Review the screenshot for a better idea of this concept (see Figure 1).

Creating a registration image on the fly
Figure 1

For demo purposes I am generating an image on the button click event. But you may generate the image on the page load event of the registration form.

Ok back on coding, let's go over the following steps we perform in the sample code.

Step 1: Create a new web application

Step 2: Open Code Behind Form of web application you have created.

Step 3: Add Namespace for drawing Image.

using System.Drawing;
using System.Drawing.Imaging;

Step 4: Write the Code for Generating a Random Registration String (Here: Generating Random string of 10 characters in length).

    // Generate a random password with the specified length. The password will only 
    // contain digits and letters (either lowercase or uppercase)
    public static string GetRandomPassword(int length)
    {
        Random rand = new Random();
        System.Text.StringBuilder password = new System.Text.StringBuilder(length);
        for (int i = 1; i <= length; i++)
        {
            int charIndex;
            // allow only digits and letters
            do
            {
                charIndex = rand.Next(48, 123);
            } while (!((charIndex >= 48 && charIndex <= 57) ||
            (charIndex >= 65 && charIndex <= 90) || (charIndex >= 97 && charIndex <= 122)));


            // add the random char to the password being built
            password.Append(Convert.ToChar(charIndex)); 

        }
        return password.ToString();
    }

Step 5: Write Code for Generating Image. This code will create RegistrationImg.gif in your application folder.

    //Generate Image
    private void GenerateImage(string strRegistrationStr)
    {
        Bitmap objBitmap = new Bitmap(150, 30);
        Graphics objGraphics = Graphics.FromImage(objBitmap);
 

        //Fore Color and Background of Image
        SolidBrush objForeColor = new SolidBrush(Color.White);
        SolidBrush objBackColor = new SolidBrush(Color.Black);
 
        objGraphics.FillRectangle(objBackColor, 0, 0, 150, 30); 

        //Font settings for Image
        Font objFont = new Font("Arial", 15); 

        //Display from 5point from x-axis and 5point from y-axis
        Point objPoint = new Point(5, 5); 

        //Drawing Registration String
        objGraphics.DrawString(strRegistrationStr, objFont, objForeColor, objPoint);

        //Saving Registration String as Image
        //If you dont want image to save
        //objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
        //otherwise use this
        objBitmap.Save(Server.MapPath("RegistrationImg.gif"), ImageFormat.Gif);        


        //Release object
        if(objBitmap != null)
            objBitmap.Dispose();
        if (objGraphics != null)
            objGraphics.Dispose();
    }

Step 6: Finally the code for calling the created functionality.

   protected void btnShowRegistrationImg_Click(object sender, EventArgs e)
    {
        GenerateImage(GetRandomPassword(10));
        ImgRegistrationStr.ImageUrl = "RegistrationImg.gif";
    }

DotNetGuts (DNG) "Lets Make Programming easy"
Website : http://www.DotNetGuts.2ya.com
Blog : http://www.DotNetGuts.Blogspot.com
Group : http://www.groups.yahoo.com/dotnetguts

Download File: RegisImageOnFLYCODE.zip
Size: 12.26 KB, Type: application/x-zip-compressed


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 6/21/2009 3:54:07 AM by Anonymous
Comments: Good
No ratings available.
Left on 3/8/2009 4:53:12 PM by Anonymous
Comments: Good Day. The release of atomic energy has not created a new problem. It has merely made more urgent the necessity of solving an existing one.
I am from Laos and now study English, give true I wrote the following sentence: "I’m not going to discuss the problem of whether artists should be allowed to make money off their work."

With love 8-), Tem.
Left on 2/7/2008 5:38:51 AM by Anonymous
Comments:
No ratings available.
     

 

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     

     
     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use