asp.net (C #) Generate verification code click to refresh
Source: Internet
Author: User
First build a page to generate the verification code checkcode.aspx:
The CheckCode.aspx.cs code is as follows
To add a reference:
Using System.IO;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Drawing.Drawing2D;
public partial class Default2:System.Web.UI.Page
{
Private Bitmap validateimage;
Private Graphics G;
protected void Page_Load (object sender, EventArgs e)
{
Response.bufferoutput = true; Special attention
Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1));//Special attention
Response.Cache.SetCacheability (Httpcacheability.nocache)/special attention
Response.appendheader ("Pragma", "No-cache"); Special attention
String vnum = Makevalidatecode ();
session["Vnum"] = vnum;//Obtain the verification code for later verification
Validatecode (Vnum);
}
public void Validatecode (string vnum)
{
Validateimage = new Bitmap (A, PIXELFORMAT.FORMAT24BPPRGB);
g = Graphics.fromimage (validateimage);
G.fillrectangle (New LinearGradientBrush (0, 0), New Point (a), Color.FromArgb (m, 255, 255, 255), color.fr Omargb (240, 255, 255, 255)), 0, 0, 200, 200); Rectangular box
g.DrawString (vnum, new Font ("Arial", one), New SolidBrush (color.red), New PointF (6, 0));//font/color
G.save ();
MemoryStream ms = new MemoryStream ();
Validateimage. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
response.c;
Response.BinaryWrite (Ms. ToArray ());
Response.End ();
}
String Makevalidatecode ()
{
Char[] s = new char[] {' 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 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' Q ', ' I ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};//enumerated array
String num = "";
Random r = new Random ();
for (int i = 0; i < 5; i++)
{
num + + s[r.next (0, S.length)]. ToString ();
}
return num;
}
}
The HTML of the page that displays the captcha is then added
Before <script type= "Text/javascript" >
function Reloadcode () {
document.getElementById ("Yzm"). src = "checkcode.aspx";
}
</script>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.