Now many web sites use the random image of the way, is a simple, effective means of preventing malicious attacks. Today, I read some information on the Internet to understand its generative principle: from the sample, get the random string, the random string is saved into the session, and a bitmap form random code picture.
Realize:
Add namespaces
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 picture
Setvalidatecode ();
//Build page not saved to cache
Response.Cache.SetNoStore ();
}
//Set Verification code
private void Setvalidatecode ()
{
//New bitmap
Bitmap newbitmap = new Bitmap (
71,
23,
Pixelformat.format32bppargb
);
//Get drawing picture from bitmap
Graphics g = graphics.fromimage (Newbitmap);
//random number generator
Random r = new Random ();
//Drawing screen empty
g.clear (Color.White);
//Drawing screen interference
for (int i = 0; i < 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 points interference
for (int i = 0; i < i++)
{
int x = R.next (newbitmap.width);
int y = r.next (newbitmap.height);
Newbitmap.setpixel (
x,
y,
Color.FromArgb (R.next ())
);
}
//Get random string (5-bit length)
String value = Generaterandom (5);
//Random string assignment to session
session["Randcode"] = value;
//Definition picture Display font style
font font = new Font (
"Arial",
14,
FontStyle.Bold
);
Random rr = new Random ();
int yy = RR. Next (1, 4);
//define random string display picture 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,
22
);
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 ();
}
}
Using a random picture page, the image control is written as follows:
<asp:image id= "Image1" imageurl= "~/getrandimg.aspx" runat= "Server"/>
Sample code: Http://www.cnblogs.com/Files/heekui/RandCode.rar