asp.net mvc 驗證碼

來源:互聯網
上載者:User

標籤:style   blog   color   os   資料   io   for   cti   

  1 #region  產生驗證碼圖片  2         //[OutputCache(Location = OutputCacheLocation.None, Duration = 0, NoStore = false)]  3         public ActionResult SecurityCode()  4         {  5   6             string oldcode = TempData["SecurityCode"] as string;  7             string code = CreateRandomCode(5);  8             TempData["SecurityCode"] = code;  9             return File(CreateValidateGraphic(code), "image/Jpeg"); 10         } 11  12  13         private byte[] CreateImage(string checkCode) 14         { 15             int iwidth = (int)(checkCode.Length * 12); 16             System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); 17             Graphics g = Graphics.FromImage(image); 18             Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold); 19             Brush b = new System.Drawing.SolidBrush(Color.White); 20             g.Clear(Color.Blue); 21             g.DrawString(checkCode, f, b, 3, 3); 22             Pen blackPen = new Pen(Color.Black, 0); 23             Random rand = new Random(); 24             for (int i = 0; i < 5; i++) 25             { 26                 int x1 = rand.Next(image.Width); 27                 int x2 = rand.Next(image.Width); 28                 int y1 = rand.Next(image.Height); 29                 int y2 = rand.Next(image.Height); 30                 g.DrawLine(new Pen(Color.Silver, 1), x1, y1, x2, y2); 31             } 32             System.IO.MemoryStream ms = new System.IO.MemoryStream(); 33             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 34             return ms.ToArray(); 35         } 36  37         private string CreateRandomCode(int codeCount) 38         { 39             string allChar = "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,W,X,Y,Z"; 40             string[] allCharArray = allChar.Split(‘,‘); 41             string randomCode = ""; 42             int temp = -1; 43             Random rand = new Random(); 44             for (int i = 0; i < codeCount; i++) 45             { 46                 if (temp != -1) 47                 { 48                     rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); 49                 } 50                 int t = rand.Next(35); 51                 if (temp == t) 52                 { 53                     return CreateRandomCode(codeCount); 54                 } 55                 temp = t; 56                 randomCode += allCharArray[t]; 57             } 58             return randomCode; 59         } 60         /// <summary> 61         /// 建立驗證碼的圖片 62         /// </summary> 63         public byte[] CreateValidateGraphic(string validateCode) 64         { 65             Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27); 66             Graphics g = Graphics.FromImage(image); 67             try 68             { 69                 //產生隨機產生器 70                 Random random = new Random(); 71                 //清空圖片背景色 72                 g.Clear(Color.White); 73                 //畫圖片的幹擾線 74                 for (int i = 0; i < 25; i++) 75                 { 76                     int x1 = random.Next(image.Width); 77                     int x2 = random.Next(image.Width); 78                     int y1 = random.Next(image.Height); 79                     int y2 = random.Next(image.Height); 80                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 81                 } 82                 Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic)); 83                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 84                  Color.Blue, Color.DarkRed, 1.2f, true); 85                 g.DrawString(validateCode, font, brush, 3, 2); 86                 //畫圖片的前景幹擾點 87                 for (int i = 0; i < 100; i++) 88                 { 89                     int x = random.Next(image.Width); 90                     int y = random.Next(image.Height); 91                     image.SetPixel(x, y, Color.FromArgb(random.Next())); 92                 } 93                 //畫圖片的邊框線 94                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 95                 //儲存圖片資料 96                 MemoryStream stream = new MemoryStream(); 97                 image.Save(stream, ImageFormat.Jpeg); 98                 //輸出圖片流 99                 return stream.ToArray();100             }101             finally102             {103                 g.Dispose();104                 image.Dispose();105             }106         }107         #endregion

 

聯繫我們

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