C# GDI繪製驗證碼

來源:互聯網
上載者:User

標籤:

步驟:1.通過Random產生隨機數或字元及驗證碼2.通過驗證碼內容長度產生指定大小的圖片3.擷取產生圖片的Graphics對象4.定義驗證碼字型格式5.通過指定字型將驗證碼繪製到圖片6.向圖片上添加背景雜音線7.添加前景噪音點 
 1  private void pictureBox1_Click(object sender, EventArgs e) 2         { 3             Random r = new Random(); 4             string str = null; 5             for (int i = 0; i < 5; i++) 6             { 7                 int rNumber = r.Next(0, 10); 8                 str += rNumber; 9             }10             //MessageBox.Show(str);11             //建立GDI對象12             Bitmap bmp=new Bitmap(120,50);13             Graphics g = Graphics.FromImage(bmp);14 15             for (int i = 0; i < 5; i++)16             {17                 Point p=new Point(i*20,0);18                 string [] fonts={"微軟雅黑","宋體","黑體","隸書","仿宋"};19                 Color [] colors={Color.Yellow,Color.Blue,Color.Black,Color.Red,Color.Green};20                 g.DrawString(str[i].ToString(),new Font(fonts[r.Next(0,4)],20,FontStyle.Bold),new SolidBrush(colors[r.Next(0,5)]),p);21             }22             //繪製線條,線條不能超出驗證碼的範圍23             for (int i = 0; i < 30; i++)24             {25                 Point p1=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));26                 Point p2=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));27                 g.DrawLine(new Pen(Brushes.Green), p1, p2);28             }29 30             for (int i = 0; i < 1000; i++)31             {32                 Point p=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));33                 bmp.SetPixel(p.X, p.Y, Color.Black);34             }35             //將圖片鑲嵌到PictureBox中36             pictureBox1.Image = bmp;37 38         }
View Code

 

C# GDI繪製驗證碼

聯繫我們

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