如何在ASP.NET中实现验证码?

如题所述

1.知识点介绍验证码其实是随机产生的一些数字,在System命名空间下提供的Random类可以用来产生随机性的非负数字.在ASP.NET中的System.Drawing命名空间中,提供了Bitmap,Graphics类,其中Bitmap类封装了GDI+位图,继承自Image类,用于处理由像素数据定义的图象.Graphics类封装了GDI和绘图面,也就是相当于画板.下面就使用Random,Bitmap和Graphics类把生成的随即数绘制到创建的位图中.并用Image控件显示创建的位图.2.示例新建"WebForm1.aspx"页面,在该页面中添加一个Image控件,并设置其ImageUrl属性为生成的位图所要存放的路径.(1)WebForm1.aspx.cs代码:private void Page_Load(object sender, System.EventArgs e){//建立位图对象 Bitmap newBitmap=new Bitmap(36,16,System.Drawing.Imaging.PixelFormat.Format32bppArgb);//根据上面创建的位图对象创建绘图层 Graphics g=Graphics.FromImage(newBitmap);//以指定的颜色填宠矩形区 g.FillRectangle(new SolidBrush(Color.White),new Rectangle(0,0,36,16));//创建字体对象 Font textFont=new Font("Times new Roman",10);//创建RectangleF结构指定一个区域 RectangleF rectangle=new RectangleF(0,0,36,16);//创建随机数对象 Random rd=new Random();
温馨提示:答案为网友推荐,仅供参考
相似回答