C# 產生簡單驗證碼

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   io   

網站登入總是會用到驗證碼,產生驗證碼對於C#來說很簡單。因為有專門封裝好的GDI+類可以直接調用使用具體代碼如下

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Drawing; 8  9 namespace Ajax.驗證碼10 {11     public partial class Yanma : System.Web.UI.Page12     {13         private int CodeNum = 4;14         private string codeNum = "";15         protected void Page_Load(object sender, EventArgs e)16         {17             VeriflcationCode();18             HuaCode(codeNum);19         }20 21         private void VeriflcationCode()22         {23             //產生驗證碼24             string AllChar = "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,W,X,Y,Z";25             string[] StrChar = AllChar.Split(‘,‘);26             Random rd = new Random();27             string Codes = "";28             for (int i = 0; i < CodeNum; i++)29             {30                 Codes += StrChar[rd.Next(0, 35)];31             }32             codeNum = Codes;33             Session["VerCode"] = Codes; 34         }35 36         private void HuaCode(string Codes)37         {38             int iwidth = (int)(Codes.Length * 17);//定義畫布的寬39             System.Drawing.Bitmap images = new System.Drawing.Bitmap(iwidth, 25);//聲明一個畫布類,初始化圖片的寬和高40 41             Graphics g = Graphics.FromImage(images);42             Font f = new Font("Arial", 14);//定義文字類型43             Brush b = new System.Drawing.SolidBrush(Color.Black);//文字顏色 黑色44             Brush r = new System.Drawing.SolidBrush(Color.FromArgb(166, 8, 8));//文字顏色 45             g.Clear(System.Drawing.ColorTranslator.FromHtml("#99C1CB"));//背景色46 47             char[] ch = Codes.ToCharArray();48             for (int i = 0; i < ch.Length; i++)49             {50                 if (ch[i] >= ‘0‘ && ch[i] <= ‘9‘)51                 {52                     //數字用紅色顯示 53                     g.DrawString(ch[i].ToString(), f, r, 3 + (i * 14), 3);//將文字畫出 文字、字型類型、字型顏色、字型間距、字型於頂部間距54                 }55                 else56                 {   //字母用黑色顯示57                     g.DrawString(ch[i].ToString(), f, b, 3 + (i * 14), 3);58                 }59             }60 61             //for迴圈用來產生一些隨機的水平線62             Pen blackPen = new Pen(Color.Black, 0);63             Random rand = new Random();64             for (int i = 0; i < 5; i++)65             {66                 int y = rand.Next(images.Height);67                 g.DrawLine(blackPen, 0, y, images.Width, y);68             }69 70 71             System.IO.MemoryStream ms = new System.IO.MemoryStream();72             images.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);73             //history back 不重複 74             Response.Cache.SetNoStore();//這一句         75             Response.ClearContent();76             Response.ContentType = "image/Jpeg";77             Response.BinaryWrite(ms.ToArray());78             g.Dispose();79             images.Dispose();80         }

效果為:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.