Many web pages now use random images when logging in, which is a simple and effective way to prevent malicious attacks by hackers. I read some information on the Internet today and understand the generation principle: get a random string from the sample, save the random string into the session, and form a random code picture in the form of a bitmap.
accomplish:
Add namespace
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
Generate page code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class getRandImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Generate random code pictures
SetValidateCode();
//The generated page is not saved to cache
Response.Cache.SetNoStore();
}
//Set verification code
private void SetValidateCode()
{
//Create a new bitmap
Bitmap newBitmap = new Bitmap(
71,
twenty three,
PixelFormat.Format32bppArgb
);
//Get the drawing screen from the bitmap
Graphics g = Graphics.FromImage(newBitmap);
//random number generator
Random r = new Random();
//Clear the drawing screen
g.Clear(Color.White);
//Drawing screen line interference
for (int i = 0; i < 50; i++)
{
int x1 = r.Next(newBitmap.Width);
int x2 = r.Next(newBitmap.Width);
int y1 = r.Next(newBitmap.Height);
int y2 = r.Next(newBitmap.Height);
g.DrawLine(new Pen(
Color.FromArgb(r.Next())),
x1,
y1,
x2,
y2
);
}
//Drawing screen point interference
for (int i = 0; i < 100; i++)
{
int x = r.Next(newBitmap.Width);
int y = r.Next(newBitmap.Height);
newBitmap.SetPixel(
x,
y,
Color.FromArgb(r.Next())
);
}
//Get a random string (5 digits in length)
string value = GenerateRandom(5);
//Assign random string to Session
Session["RandCode"] = value;
//Define image display font style
Font font = new Font(
"Arial",
14,
FontStyle.Bold
);
Random rr = new Random();
int yy = rr.Next(1, 4);
//Define a random string to display the image brush
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, 71, 23),
Color.Red,
Color.Blue,
1.2f,
true
);
g.DrawString(value, font, brush, 2, yy);
g.DrawRectangle(new Pen(
Color.Silver),
0,
0,
70,
twenty two
);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
newBitmap.Save(ms, ImageFormat.Gif);
//Output picture
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
}
//Constant set
private static char[] constant ={
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z'
};
//generate random string
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(36)]);
}
return newRandom.ToString();
}
}
For pages using random images, the IMAGE control is written as follows:
<asp:Image ID="Image1" ImageUrl="~/getRandImg.aspx" runat="server" />
Sample code: http://www.cnblogs.com /heekui/archive/2007/01/06/613609.html