繪製驗證碼

來源:互聯網
上載者:User
Code
 1     ///*************************************************
 2     /// 函數:GenerateCheckCode:產生四位隨機數
 3     ///      CreateCheckCodeImg:繪製驗證碼
 4     /// 作者:王洪劍
 5     /// 日期:2009-6-9
 6     ///*************************************************
 7     //產生隨機數
 8     private string GenerateCheckCode()
 9     {
10         int intNum;
11         char chrCode;
12         string strCheckCode = String.Empty;
13         Random random = new Random();
14         for (int i = 0; i < 4; i++)
15         {
16             intNum = random.Next();
17             if (intNum % 2 == 0)
18                 chrCode = (char)('1' + (char)(intNum % 10));
19             else
20                 chrCode = (char)('A' + (char)(intNum % 26));
21             strCheckCode += chrCode.ToString();
22         }
23         Session["HopeCheckCode"] = strCheckCode.ToString();
24         return strCheckCode.ToString();
25     }
26 
27     //繪製驗證碼
28     private void CreateCheckCodeImg(string strCheckCode)
29     {
30         if (strCheckCode == null || strCheckCode.Trim() == String.Empty)
31             return;
32         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((strCheckCode.Length * 10.5)), 20);
33         Graphics gr = Graphics.FromImage(image);
34         try
35         {
36             Random random = new Random();//產生隨機產生器
37             gr.Clear(Color.Transparent);//清空背景色並繪製白色
38             //畫圖片的背景雜音線
39             for (int i = 0; i < 2; i++)
40             {
41                 int x1 = random.Next(image.Width);
42                 int x2 = random.Next(image.Width);
43                 int y1 = random.Next(image.Height);
44                 int y2 = random.Next(image.Height);
45                 gr.DrawLine(new Pen(Color.Green), x1, x2, y1, y2);
46                 Font font = new System.Drawing.Font("Times New Roman", 12, (System.Drawing.FontStyle.Bold));//字型
47                 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);//筆刷漸層色
48                 gr.DrawString(strCheckCode, font, brush, 2, 2);
49             }
50 
51             //畫圖片前景噪音點
52             for (int i = 0; i < 30; i++)
53             {
54                 int x = random.Next(image.Width);
55                 int y = random.Next(image.Height);
56                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
57             }
58             //畫邊框線
59             gr.DrawRectangle(new Pen(Color.Transparent), 0, 0, image.Width - 1, image.Height - 1);
60             System.IO.MemoryStream ms = new System.IO.MemoryStream();
61             image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//產生圖片格式
62             Response.ClearContent();
63             Response.ContentType = "image/Png";
64             Response.BinaryWrite(ms.ToArray());
65 
66         }
67         catch (Exception ex)
68         { Response.Write("產生驗證碼失敗:" + ex.Message); }
69     }
70 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.