C# 验证码的随机生成

     来园子好久了,一直都是看别人在发博客,自己从没写过自己的博客,.net也接触有一段时间了,虽然学的不好,不过自己挺喜欢这个方向,闲来无事,借花献佛,发表自己的处子之博客,不足之处还请大牛们多多请教!

   废话不说了,下面贴出自己的代码, WinForm界面比较粗糙,但只是为了说明问题

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace CheckCode
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19         private void Form1_Load(object sender, EventArgs e)
 20         {
 21             CreateCheckCodeImage(GenerateCheckCode());
 22         }
 23 
 24         //全局变量 用来当前记录验证码
 25         private string checkCode = null;
 26 
 27         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 28         {
 29             CreateCheckCodeImage(GenerateCheckCode());
 30         }
 31 
 32         private void button1_Click(object sender, EventArgs e)
 33         {
 34             if (this.checkCode.ToLower()== textBox1.Text.ToLower())
 35             {
 36                 label1.Text = "输入正确";
 37             }
 38             else
 39             {
 40                 label1.Text = "输入错误,请重新输入!";
 41             }
 42         }
 43 
 44         /// <summary>
 45         /// 生成随机四位Ascll码字符
 46         /// </summary>
 47         /// <returns>Ascll组成的字符串</returns>
 48         private string GenerateCheckCode()
 49         {
 50             int number;
 51             char code;
 52             string checkCode = String.Empty;
 53             System.Random random = new Random();
 54 
 55             for (int i = 0; i < 4; i++)
 56             {
 57                 number = random.Next();
 58 
 59                 if (number % 2 == 0)
 60                     code = (char)('0' + (char)(number % 10));
 61 
 62                 else
 63                     code = (char)('A' + (char)(number % 26));
 64 
 65                 checkCode += code.ToString();
 66             }
 67             this.checkCode = checkCode;
 68             return checkCode;
 69         }
 70         /// <summary>
 71         /// 在Bitmap上画出对应的字符串
 72         /// </summary>
 73         /// <param name="checkCode"></param>
 74         private void CreateCheckCodeImage(string checkCode)
 75         {
 76             if (checkCode == null || checkCode.Trim() == String.Empty)
 77                 return;
 78             System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
 79             Graphics g = Graphics.FromImage(image);
 80             try
 81             {
 82                 //生成随机生成器
 83                 Random random = new Random();
 84                 //清空图片背景色
 85                 g.Clear(Color.White);
 86                 //画图片的背景噪音线
 87                 for (int i = 0; i < 2; i++)
 88                 {
 89                     int x1 = random.Next(image.Width);
 90                     int x2 = random.Next(image.Width);
 91                     int y1 = random.Next(image.Height);
 92                     int y2 = random.Next(image.Height);
 93                     g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
 94                 }
 95                 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
 96                 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
 97                 g.DrawString(checkCode, font, brush, 2, 2);
 98                 //画图片的前景噪音点
 99                 for (int i = 0; i < 100; i++)
100                 {
101                     int x = random.Next(image.Width);
102                     int y = random.Next(image.Height);
103                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
104                 }
105                 //画图片的边框线
106                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
107                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
108                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
109                 Image img = Image.FromStream(ms);
110                 pictureBox1.Image = img;
111 
112             }
113             finally
114             {
115                 g.Dispose();
116                 image.Dispose();
117             }
118         }
119       
120     }
121 }
View Code


总结:第一次写博客,代码也比较容易理解,其他的就不多说了!

 

 

 

posted on 2014-03-25 16:05  DLMUCAI付  阅读(309)  评论(0编辑  收藏  举报