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).
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