- string[] Code ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
- "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
- "1", "2", "3", "4", "5", "6", "7", "8", "9" };
- string strRd = string.Empty;
- Random rd = new Random(); //unchecked((int)DateTime.Now.Ticks)
- Bitmap Bmp = new Bitmap(150, 50); //建立實體圖檔並設定大小
- Graphics Gpi = Graphics.FromImage(Bmp);
-
- Font Font1 = new Font("Verdana", 20, FontStyle.Italic);
-
- for (int i = 0; i < 5; i++) // 亂數產生驗證文字
- {
- strRd += Code[rd.Next(34)];
- }
- Pen PenLine = new Pen(Brushes.Red, 2); //實體化筆刷並設定顏色、大小(畫X,Y軸用)
- Gpi.Clear(Color.Black); //設定背景顏色
-
- Gpi.DrawLine(PenLine, 0, rd.Next(80), 90, rd.Next(25));
- Gpi.DrawString(strRd, Font1, Brushes.AntiqueWhite, 0, 0);
- Gpi.DrawBezier(PenLine, new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)));
- for (int i = 0; i <= 25; i++) //亂數產生霧點,擾亂機器人辨別
- {
- int RandPixelX = rd.Next(0, 80);
- int RandPixelY = rd.Next(0, 25);
- Bmp.SetPixel(RandPixelX, RandPixelY, Color.Pink);
- }
-
- //Session["ValidateCode"] = strRd; //將驗證碼存入Session以便稍後進行驗
- System.IO.MemoryStream s = new System.IO.MemoryStream();
- Bmp.Save(s, System.Drawing.Imaging.ImageFormat.Gif);
- //return File(s.GetBuffer(), "image/gif");
- return Bmp;
複製代碼 |