日期:2014-05-18  浏览次数:20493 次

急求生成图片的验证码
急求一分完整无错的生成图片的验证码 c#版的

------解决方案--------------------
ASP.NET生成验证码并点击刷新验证码实例代码下载
http://bbs.hackdv.cn/showtopic-9.aspx

一个小实例源码
------解决方案--------------------
baidu
51aspx这么多现成源码
偏要到这里来问

网上类似的东西有很多的http://www.51aspx.com/S/%E9%AA%8C%E8%AF%81%E7%A0%81.html
------解决方案--------------------
1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Drawing.Design;
13using System.Drawing.Drawing2D;
14using System.Drawing.Printing;
15using System.Drawing.Imaging;
16using System.IO;
17//该源码下载自www.51aspx.com(51aspx.com)
18
19public partial class _Default : System.Web.UI.Page
20...{
21 private string CreateCheckCodeString()
22 ...{ //定义用于验证码的字符数组
23 char[] AllCheckCodeArray =...{ '0','1','2','3','4','5','6','7','8','9','A','B','C',
24 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W',
25 'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
26 'r','s','t','u','v','w','x','y','z'};
27 //定义验证码字符串
28 string randomcode = "";
29 Random rd = new Random();
30 //生成4位验证码字符串
31 for (int i = 0; i < 4; i++)
32 randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)];
33 return randomcode;
34 }
35 //生成验证码图片
36 protected void Page_Load(object sender, EventArgs e)
37 ...{
38 //定义图片的宽度
39 int ImageWidth = 55;
40 //定义图片高度
41 int ImageHeigh = 22;
42 //定义字体,用于绘制文字
43 Font font = new Font("Arial", 12, FontStyle.Bold);
44 //定义画笔,用于绘制文字
45 Brush brush = new SolidBrush(Color.Black);
46 //定义钢笔,用于绘制干扰线
47 Pen pen1 = new Pen(Color.FromArgb(255, 100, 100), 0);//这里也可以直接获得一个现有的color对象如:Color.Gold.我是为了美观所以定义和下面一样
48 Pen pen2 = new Pen(Color.FromArgb(255, 100, 100), 0);//这里根据ARGB值定义获得了一个color对象
49 //创建一个图像
50 Bitmap BitImage = new Bitmap(ImageWidth, ImageHeigh);
51 //从图像获取一个绘画面
52 Graphics graphics = Graphics.FromImage(BitImage);
53 //清除整个绘图画面并用颜色填充
54 graphics.Clear(ColorTranslator.FromHtml("#F0F0F0"));//这里从HTML代码获取color对象
55 //定义文字的绘制矩形区域
56 RectangleF rect = new RectangleF(5, 2, ImageWidth, ImageHeigh);
57 //定义一个随机数对象,用于绘制干扰线
58 Random rand = new Random();
59 //生成两条横向的干扰线
60 for (int i = 0; i < 2; i++)
61 ...{
62 //定义起点
63 Point p1 = new Point(0, rand.Next(ImageHeigh));
64 //定义终点
65 Point p2 = new Point(ImageWidth, rand.Next(ImageHeigh));
66 //绘制直线
67 graphics.DrawLine(pen1, p1, p2);
68 }
69 //生成两条纵向的干扰线
70 for (int i = 0; i < 2; i++)
71 ...{
72 //定义起点
73 Point p1 = new Point(rand.Next(ImageWidth), 0);
74 //定义终点
75 Point p2 = new Point(rand.Next(ImageWidth), ImageHeigh);
76 //绘制直线
77 graphics.DrawLine(pen2, p1, p2);
78 }
79 //绘制验证码文字
80 graphics.DrawString(CreateCheckCodeString(), font, brush, rect);
81 //保存图片为gif格式
82 BitImage.Save(Response.Outp