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.Text; public partial class AJAX_RandomJpgSample : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Session["MyCheckCode"] == null) Session["MyCheckCode"] = ""; GenerateCheckCode(); } /// <summary> /// 产生随机数 /// </summary> /// <returns></returns> private void GenerateCheckCode() { StringBuilder strS = new StringBuilder(); string strTmp = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] str = strTmp.ToCharArray(); //string[] str ={ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "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" }; Random rn = new Random(); int len = rn.Next(4, 6); //随即生成4或5,用来控制验证码的长度 Random ran = new Random(); //设置图片输出格式 Response.ContentType = "image/gif"; Bitmap b = new Bitmap(85, 28); Graphics g = Graphics.FromImage(b); g.FillRectangle(new SolidBrush(Color.YellowGreen),0,0,140,30); Font font = new Font(FontFamily.GenericSansSerif, 18, FontStyle.Bold, GraphicsUnit.Pixel); for (int i = 0; i < len; i++) { strS.Append(str[ran.Next(0,35)]); //绘出生成的随机数 g.DrawString(strS[strS.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 15, ran.Next(0, 4)); } //生成干扰条 Pen pen = new Pen(new SolidBrush(Color.Blue), 1); for (int i = 0; i < 10; i++) { g.DrawLine(pen, new Point(ran.Next(0, 199), ran.Next(0, 59)), new Point(ran.Next(0, 199), ran.Next(0, 15))); } b.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); this.Session["MyCheckCode"] = strS.ToString(); Response.End(); } /// <summary> /// 客户端验证用户输入验证码是否正确 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> public static bool CheckCode(string checkCode) { string txt = System.Web.HttpContext.Current.Session["MyCheckCode"] as string; return checkCode == txt; } }
客户端使用.aspx <img id="img4" src="RandomJpgSample.aspx" onclick="javascript:this.src='RandomJpgSample.aspx?'+Math.random()" |"ShowNew()"/> .aspx.cs if (AJAX_RandomJpgSample.CheckCode(trace.Text.Trim())) Response.Write("验证通过"); 因为web窗体(包括.aspx和.aspx.cs文件,其中.aspx继承自.aspx.cs)是一个类,所以在其它窗体可以直接使用类名调用其静态方法,但是没有IDE的智能提示支持。 单击图片刷新验证码 <script type="Text/javascript"> function ShowNew(){ var img1=document.getElementById("img3"); img1.src="RandomJpgSam.aspx?"+Math.random(); } </script> 输入一个日期判断是一年中的第几天 void Calc() { int num[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; int day,month,year,leap,i,sum=0; printf("\nPlease input year,month,day:"); scanf("%d,%d,%d",&year,&month,&day); if(year%400==0||year%4==0&&year%100!=0) leap=1; else leap=0; if(month>12||month<0) { printf("Error\n"); } else { if(day>num[leap][month-1]) printf("Error\n"); else { for(i=0;i<month-1;i++) { sum+=num[leap][i]; } sum+=day; printf("It is the %dth day of %d\n",sum,year); } } }