首先,创建一个CLASS类,然后需要add Reference的方式添加 System.Drawing(画画的类)
方法代码如下:
1
/**//// <summary>
2
/// 定义显示的随机字符
3
/// </summary>
4
/// <param name="strList"></param>
5
/// <returns></returns>
6
private string imageStr(char[] strList)
7
{
8![]()
9
if (strList == null)
10
strList = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
11
12
int codeLengh = 4;
13
string radomCode = "";
14
Random r = new Random();
15
for (int i = 0; i < codeLengh;i++)
16
{
17
radomCode += strList[r.Next(strList.Length)];
18
}
19
return radomCode;
20
}
21![]()
22
/**//// <summary>
23
/// 创建随机验证字符的IMAGE,并保存,同时返回随机字符串
24
/// </summary>
25
/// <param name="iWidth">图片宽度 0时,默认为55</param>
26
/// <param name="iHeight">图片高度 0时,默认为22</param>
27
/// <param name="font">字符字体 null时,默认为 "Arial", 12, FontStyle.Bold</param>
28
/// <param name="sb">字符颜色 null时,默认为红</param>
29
/// <param name="ImagePath">需要保存的文件绝对路径</param>
30
/// <param name="strList">随即字符库 null时,默认为0-9A-Z</param>
31
/// <returns>返回随机字符串</returns>
32
public string createImgWithStr(int iWidth,int iHeight,Font font,SolidBrush sb ,string ImagePath,char[] strList)
33
{
34
if (font == null)
35
font = new Font("Arial", 12, FontStyle.Bold);
36
if (sb == null)
37
sb = new SolidBrush(Color.Red);
38
if (iWidth == 0)
39
iWidth = 55;
40
if (iHeight == 0)
41
iHeight = 22;
42
//得到随机字符串
43
string imageString = imageStr(strList);
44
//定义横向竖向都画4跳线
45
int lineCount = 4;
46
这2支笔用来画线条的
47
Pen pen1 = new Pen(Color.Gold, 1);
48
Pen pen2 = new Pen(Color.Black, 2);
49
//定义图片
50
Bitmap image = new Bitmap(iWidth, iHeight);
51
//跟J2ME一样的画笔
52
Graphics g = Graphics.FromImage(image);
53
//先画背景色 当然你可以自定义下
54
g.Clear(ColorTranslator.FromHtml("#F0F0F0"));
55
//确定写字的落点
56
Rectangle rect = new Rectangle(5, 2, iWidth, iHeight);
57![]()
58
Random r = new Random();
59![]()
60
//默认随机画横向竖向4条线
61
for(int i =0;i<lineCount;i++)
62
{
63
Point p1 = new Point(0, r.Next(iHeight));
64
Point p2 = new Point(iWidth, r.Next(iHeight));
65
Point p3 = new Point(r.Next(iWidth), 0);
66
Point p4 = new Point(r.Next(iWidth), iHeight);
67
g.DrawLine(pen1, p1, p2);
68
g.DrawLine(pen2, p3, p4);
69
}
70
//写字
71
g.DrawString(imageString, font, sb, rect);
72
//删除源文件
73
if (File.Exists(ImagePath))
74
File.Delete(ImagePath);
75
//保存文件,我定义为jpeg格式
76
image.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
77
//释放资源
78
g.Dispose();
79
image.Dispose();
80![]()
81
return imageString;
82
}
/**//// <summary>2
/// 定义显示的随机字符3
/// </summary>4
/// <param name="strList"></param>5
/// <returns></returns>6
private string imageStr(char[] strList)7
{8

9
if (strList == null)10
strList = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();11
12
int codeLengh = 4;13
string radomCode = "";14
Random r = new Random();15
for (int i = 0; i < codeLengh;i++)16
{17
radomCode += strList[r.Next(strList.Length)];18
}19
return radomCode;20
}21

22
/**//// <summary>23
/// 创建随机验证字符的IMAGE,并保存,同时返回随机字符串24
/// </summary>25
/// <param name="iWidth">图片宽度 0时,默认为55</param>26
/// <param name="iHeight">图片高度 0时,默认为22</param>27
/// <param name="font">字符字体 null时,默认为 "Arial", 12, FontStyle.Bold</param>28
/// <param name="sb">字符颜色 null时,默认为红</param>29
/// <param name="ImagePath">需要保存的文件绝对路径</param>30
/// <param name="strList">随即字符库 null时,默认为0-9A-Z</param>31
/// <returns>返回随机字符串</returns>32
public string createImgWithStr(int iWidth,int iHeight,Font font,SolidBrush sb ,string ImagePath,char[] strList)33
{34
if (font == null)35
font = new Font("Arial", 12, FontStyle.Bold);36
if (sb == null)37
sb = new SolidBrush(Color.Red);38
if (iWidth == 0)39
iWidth = 55;40
if (iHeight == 0)41
iHeight = 22;42
//得到随机字符串43
string imageString = imageStr(strList);44
//定义横向竖向都画4跳线45
int lineCount = 4;46
这2支笔用来画线条的47
Pen pen1 = new Pen(Color.Gold, 1);48
Pen pen2 = new Pen(Color.Black, 2);49
//定义图片50
Bitmap image = new Bitmap(iWidth, iHeight);51
//跟J2ME一样的画笔52
Graphics g = Graphics.FromImage(image);53
//先画背景色 当然你可以自定义下54
g.Clear(ColorTranslator.FromHtml("#F0F0F0"));55
//确定写字的落点56
Rectangle rect = new Rectangle(5, 2, iWidth, iHeight);57

58
Random r = new Random();59

60
//默认随机画横向竖向4条线61
for(int i =0;i<lineCount;i++)62
{63
Point p1 = new Point(0, r.Next(iHeight));64
Point p2 = new Point(iWidth, r.Next(iHeight));65
Point p3 = new Point(r.Next(iWidth), 0);66
Point p4 = new Point(r.Next(iWidth), iHeight);67
g.DrawLine(pen1, p1, p2);68
g.DrawLine(pen2, p3, p4);69
}70
//写字71
g.DrawString(imageString, font, sb, rect);72
//删除源文件73
if (File.Exists(ImagePath))74
File.Delete(ImagePath);75
//保存文件,我定义为jpeg格式76
image.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);77
//释放资源78
g.Dispose();79
image.Dispose();80

81
return imageString;82
}另外,我在实际运用过程中总是发现重新生成了图片,但是显示却还是以前那张,最后在网上找到了答案:
原本的<asp:Image ID="Image1" runat="server" ImageUrl="~/a.jpg" />
改成<asp:Image ID="Image1" runat="server" ImageUrl="~/a.jpg?temp=<%= DateTime.Now.Ticks%>" />
就能够自动更新了。。

浙公网安备 33010602011771号