|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
也许C#刚上市的时候有些抄袭Java吧,但自从C#2.0上市之后,整个局面就扭转乾坤了,不但Java在模仿C#,而且他从来都没能跟得上C#的脚步。asp.net|考证码背景文件:
1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Collections;
5usingSystem.Drawing;
6usingSystem.Web;
7usingSystem.Web.Security;
8usingSystem.Web.UI;
9usingSystem.Web.UI.WebControls;
10usingSystem.Web.UI.WebControls.WebParts;
11usingSystem.Web.UI.HtmlControls;
12
13publicpartialclassDefault2:System.Web.UI.Page
14{
15protectedvoidPage_Load(objectsender,EventArgse)
16{
17if(!this.IsPostBack)
18{
19this.GenImg(this.GenCode(4));
20}
21
22}
23//发生随机字符串
24privatestringGenCode(intnum)
25{
26string[]source={"0","1","2","3","4","5","6","7","8","9",
27"A","B","C","D","E","F","G","H","I","J","K","L","M","N",
28"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
29stringcode="";
30Randomrd=newRandom();
31for(inti=0;i<num;i++)
32{
33code+=source[rd.Next(0,source.Length)];
34}
35returncode;
36}
37
38//天生图片
39privatevoidGenImg(stringcode)
40{
41BitmapmyPalette=newBitmap(60,20);//界说一个画板
42
43Graphicsgh=Graphics.FromImage(myPalette);//在画板上界说画图的实例
44
45Rectanglerc=newRectangle(0,0,60,20);//界说一个矩形
46
47gh.FillRectangle(newSolidBrush(Color.Blue),rc);//添补矩形
48gh.DrawString(code,newFont("宋体",16),newSolidBrush(Color.White),rc);//在矩形内画出字符串
49
50myPalette.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显现出来
51
52Session["ValidateCode"]=code;//将字符串保留到Session中,以便必要时举行考证
53
54gh.Dispose();
55myPalette.Dispose();
56}
57
58}
HTML文件:
到场一个HTML控件Image便可
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>
<imgsrc="Default2.aspx"/>
在CSDN里搜索一下“初学”两字,竟有三百余篇帖子(也许更多)。有些帖子说,有了asp的基础,只要15天就能很熟悉了,我甚感自己的愚钝。更多帖子是向大家请教初学者适合看书。两个多月的时间(当然平常杂事比较多。 |
|