深入c#繪制驗(yàn)證碼的詳解
更新時(shí)間:2013年06月08日 15:23:26 作者:
本篇文章是對(duì)用c#繪制驗(yàn)證碼的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
1.使用一個(gè)PictureBox空間,使用一個(gè)按鈕,以刷新驗(yàn)證碼。
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()方法生成的序列轉(zhuǎn)化為圖片并顯示:
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ù)字組成的字符串序列:
復(fù)制代碼 代碼如下:
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()方法生成的序列轉(zhuǎn)化為圖片并顯示:
復(fù)制代碼 代碼如下:
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.
復(fù)制代碼 代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
相關(guān)文章
C#調(diào)用Python程序傳參數(shù)獲得返回值
C# 調(diào)用 Python 程序有多種方式,本文主要介紹了4種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02c#計(jì)算某段代碼的執(zhí)行時(shí)間實(shí)例方法
在本篇文章里我們給大家整理了關(guān)于c#計(jì)算某段代碼的執(zhí)行時(shí)間的方法和經(jīng)驗(yàn),有興趣的朋友們學(xué)習(xí)下。2019-02-02C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#枚舉類型與結(jié)構(gòu)類型實(shí)例解析
這篇文章主要介紹了C#枚舉類型與結(jié)構(gòu)類型實(shí)例,需要的朋友可以參考下2014-07-07C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝示例
這篇文章主要介紹了C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝,分析了微信網(wǎng)頁(yè)授權(quán)操作的原理、步驟并給出了C#實(shí)現(xiàn)的網(wǎng)頁(yè)授權(quán)操作邏輯封裝類,需要的朋友可以參考下2016-10-10C#實(shí)現(xiàn)滑動(dòng)開(kāi)關(guān)效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)滑動(dòng)開(kāi)關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07