深入c#繪制驗證碼的詳解
更新時間:2013年06月08日 15:23:26 作者:
本篇文章是對用c#繪制驗證碼的方法進行了詳細的分析介紹,需要的朋友參考下
1.使用一個PictureBox空間,使用一個按鈕,以刷新驗證碼。
2.首先定義CheckCode()方法,以生成4為英文及數(shù)字組成的字符串序列:
private string CheckCode()
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
number=random.Next();
if(number%2==0)
code=(char)('0'+(char)(number%10));
else
code=(char)('A'+(char)(number%26));
checkCode += " " + code.ToString();
}
return checkCode;
}
3.自定義CodeImage()方法,將CheckCode()方法生成的序列轉化為圖片并顯示:
private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
System.Drawing.Bitmap image=new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length*50.0)),50);
Graphics g=Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 3; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 30, (System.Drawing.FontStyle.Bold));
g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
for (int i = 0; i < 200; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
this.pictureBox1.Width = image.Width;
this.pictureBox1.Height = image.Height;
this.pictureBox1.BackgroundImage = image;
}
catch
{
}
}
4.
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
2.首先定義CheckCode()方法,以生成4為英文及數(shù)字組成的字符串序列:
復制代碼 代碼如下:
private string CheckCode()
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
number=random.Next();
if(number%2==0)
code=(char)('0'+(char)(number%10));
else
code=(char)('A'+(char)(number%26));
checkCode += " " + code.ToString();
}
return checkCode;
}
3.自定義CodeImage()方法,將CheckCode()方法生成的序列轉化為圖片并顯示:
復制代碼 代碼如下:
private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
System.Drawing.Bitmap image=new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length*50.0)),50);
Graphics g=Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 3; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 30, (System.Drawing.FontStyle.Bold));
g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
for (int i = 0; i < 200; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
this.pictureBox1.Width = image.Width;
this.pictureBox1.Height = image.Height;
this.pictureBox1.BackgroundImage = image;
}
catch
{
}
}
4.
復制代碼 代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
相關文章
C#實現(xiàn)把圖片轉換成二進制以及把二進制轉換成圖片的方法示例
這篇文章主要介紹了C#實現(xiàn)把圖片轉換成二進制以及把二進制轉換成圖片的方法,結合具體實例形式分析了基于C#的圖片與二進制相互轉換以及圖片保存到數(shù)據(jù)庫的相關操作技巧,需要的朋友可以參考下2017-06-06
unity實現(xiàn)虛擬搖桿控制Virtual Joystick
這篇文章主要為大家詳細介紹了unity實現(xiàn)虛擬搖桿控制Virtual Joystick,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
linux操作系統(tǒng)安裝MONO執(zhí)行C#程序的詳解步驟
這篇文章主要介紹了linux操作系統(tǒng)安裝MONO執(zhí)行C#程序詳解步驟,有需要的可以參考一下2013-12-12

