首先
方法代碼如下
/**//// <summary>
/// 定義顯示的隨機字符
/// </summary>
/// <param name=
/// <returns></returns>
private string imageStr(char[] strList)
if (strList == null)
strList =
int codeLengh =
string radomCode =
Random r = new Random();
for (int i =
radomCode += strList[r
}
return radomCode;
}
/**//// <summary>
/// 創建隨機驗證字符的IMAGE
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <returns>返回隨機字符串</returns>
public string createImgWithStr(int iWidth
if (font == null)
font = new Font(
if (sb == null)
sb = new SolidBrush(Color
if (iWidth ==
iWidth =
if (iHeight ==
iHeight =
//得到隨機字符串
string imageString = imageStr(strList);
//定義橫向豎向都畫
int lineCount =
這
Pen pen
Pen pen
//定義圖片
Bitmap image = new Bitmap(iWidth
//跟J
Graphics g = Graphics
//先畫背景色 當然你可以自定義下
g
//確定寫字的落點
Rectangle rect = new Rectangle(
Random r = new Random();
//默認隨機畫橫向豎向
for(int i =
Point p
Point p
Point p
Point p
g
g
}
//寫字
g
//刪除源文件
if (File
File
//保存文件
image
//釋放資源
g
image
return imageString;
}
另外
From:http://tw.wingwit.com/Article/program/net/201311/15641.html