首先,创建一个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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
58
Random r = new Random();
59![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
81
return imageString;
82
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
另外,我在实际运用过程中总是发现重新生成了图片,但是显示却还是以前那张,最后在网上找到了答案:
原本的<asp:Image ID="Image1" runat="server" ImageUrl="~/a.jpg" />
改成<asp:Image ID="Image1" runat="server" ImageUrl="~/a.jpg?temp=<%= DateTime.Now.Ticks%>" />
就能够自动更新了。。