using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;u ...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GDI_
{
public partial class 數字母 : Form
{
public 數字母()
{
InitializeComponent();
}
//在WinForm窗體在實現隨機驗證碼
private void pictureBox1_Click(object sender, EventArgs e)
{
//定義畫布大小
Bitmap bmp = new Bitmap(100, 100);
//定義一個變數
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random r = new Random (); //隨機生成
string newstr = "";
for(int i=0;i<6;i++)
{
newstr += str[r.Next(0, 62)];
}
Graphics g = Graphics.FromImage(bmp);
//隨機字體
String[] fname = { "仿宋", "黑體", "宋體", "楷體", "微軟雅黑", "華文行楷" };
//隨機顏色
Color[] color = { Color.Red, Color.Green, Color.Pink, Color.Yellow, Color.Gold, Color.Black };
for(int i=0;i<newstr.Length;i++)
{
Point p = new Point(i * 15, 20);
g.DrawString(newstr[i].ToString(), new Font(fname[r.Next(0, 6)], 18, FontStyle.Italic), new SolidBrush(color[r.Next(0, 6)]), p);
}
// 驗證碼上畫圖片的背景噪音線
for (int i=0;i<15;i++)
{
g.DrawLine(new Pen(color[r.Next(0, 6)]), new Point(r.Next(0, 100), r.Next(0, 100)), new Point(r.Next(0, 100), r.Next(0, 100)));
}
// 驗證碼上畫圖片的背景噪音點
for (int i=0;i<300;i++)
{
bmp.SetPixel(r.Next(0, 100), r.Next(0, 100), color[r.Next(0, 6)]);
}
this.pictureBox1.Image = bmp;
}