使用GDI繪製驗證碼

2021-07-02 00:07:38 字數 726 閱讀 5901

首先建立乙個c# windows窗體應用程式

然後在窗體中新增乙個picturebox和button控制項

接著給button按鈕註冊乙個單機事件,開始寫**

void identifyingcode()

//建立位圖物件,設定的寬度和高度,bitmap繼承了image

bitmap bmp = new bitmap(110, 30);

//建立gdi物件,fromimage引數需要的是image物件,bitmap繼承了image,所以可以用bitmap物件做引數

graphics g = graphics.fromimage(bmp);

//指定驗證碼的隨機字型

string fonts = ;

//指定驗證碼的隨機顏色

color colors = ;

//將產生的字串畫到上去

for (int i = 0; i < 5; i++)

//在中畫直線

for (int i = 0; i < 20; i++)

for (int i = 0; i < 300; i++)

//將鑲嵌到picturebox中

picturebox1.image = bmp;

}

效果如圖:

C 利用GDI繪製驗證碼

片 using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text...

繪製驗證碼

public partial class validatecode validatebase 生成驗證碼 private string createvalidatecode base.strvalidate validatecode 儲存驗證碼 return validatecode 影象背景 pr...

驗證碼繪製

驗證碼繪製 1.產生驗證碼字串 使用ascii碼和sprintf函式生成驗證碼字串 ascii碼對應字元如下 數字 48 57 小寫字母 97 122 大寫字母 65 90 sprintf c ascii 將ascii碼轉為對應字元2.繪製驗證碼 繪製步驟 1 建立畫布 2 建立畫筆 3 填充背景色...