c#实现验证码 源码

时间:2012-04-22 14:01:07
【文件属性】:
文件名称:c#实现验证码 源码
文件大小:891B
文件格式:RAR
更新时间:2012-04-22 14:01:07
验证码 public partial class image : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.GenImg(this.GenCode(4)); ////将验证码存储到session中,以便需要时进行验证 Session["image"] = this.GenCode(4); } //任意产生4个验证码 private string GenCode(int num) { //定义一个验证码数组 string[] source ={ "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" }; string code = ""; Random rd = new Random(); for (int i = 0; i < num; i++) { code += source[rd.Next(0, source.Length)]; } return code; } //生成图片 private void GenImg(string code) { //定义一个画板 Bitmap myPalette = new Bitmap(60, 20); //在画板上定义绘图的实例 Graphics gh = Graphics.FromImage(myPalette); //定义一个矩形 Rectangle rc = new Rectangle(0, 0, 60, 20); //填充矩形 gh.FillRectangle(new SolidBrush(Color.Blue), rc); //在矩形内画出字符串 gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.White), rc); //将图片显示出来 myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); gh.Dispose(); myPalette.Dispose(); } }
【文件预览】:
c#验证码.txt

网友评论

  • 资源可用,稍改了一下感觉不错,解决问题了。
  • 下载下来的,和资源简介中的一样的,冤死了~
  • 没什么用,感觉很乱
  • 给个能实现的代码吧,你这个用处不大
  • 很好的资料,解决了大问题,直接可以使用的