急求生成图片的验证码
急求一分完整无错的生成图片的验证码 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.OutputStream, ImageFormat.Gif);
83 //释放对象
84 graphics.Dispose();
85 BitImage.Dispose();
86 }
87}
88
[解决办法]
新建一个aspx页面
using System;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Drawing;public partial class valicode : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { HttpContext.Current.Session["CheckCode"] = RndNum(4); CreateImages(HttpContext.Current.Session["CheckCode"].ToString()); } /// <summary> /// 生成验证图片 /// </summary> /// <param name="checkCode">验证字符</param> private void CreateImages(string checkCode) { int iwidth = (int)(checkCode.Length * 13); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; Random rand = new Random(); //随机输出噪点 for (int i = 0; i < 50; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //输出不同字体和颜色的验证码字符 for (int i = 0; i < checkCode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii); } //画一个边框 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); HttpContext.Current.Response.ClearContent(); //Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Jpeg"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } /// <summary> /// 生成随机的字母 /// </summary> /// <param name="VcodeNum">生成字母的个数</param> /// <returns>string</returns> private string RndNum(int VcodeNum) { string Vchar = "0,1,2,3,4,5,6,7,8,9"; string[] VcArray = Vchar.Split(','); string VNum = ""; //由于字符串很短,就不用StringBuilder了 int temp = -1; //记录上次随机数值,尽量避免生产几个一样的随机数 //采用一个简单的算法以保证生成随机数的不同 Random rand = new Random(); for (int i = 1; i < VcodeNum + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(VcArray.Length); if (temp != -1 && temp == t) { return RndNum(VcodeNum); } temp = t; VNum += VcArray[t]; } return VNum; }}
[解决办法]
网上多的是,去搜一下就可以了
看了一下8楼的代码,也试了一下,可行的,但真不知道它为什么要将前面的行数也标记上,让我删了好久
给你一个具体过程,自己添加就可以了
1.单独添加一个aspx页面,前台不填任何内容,后台将这样的代码复制进去
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing;using System.Drawing.Design;using System.Drawing.Drawing2D;using System.Drawing.Printing;using System.Drawing.Imaging;using System.IO;public partial class _Default : System.Web.UI.Page{ private string CreateCheckCodeString() { //定义用于验证码的字符数组 char[] AllCheckCodeArray ={ '0','1','2','3','4','5','6','7','8','9','A','B','C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', 'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q', 'r','s','t','u','v','w','x','y','z'}; //定义验证码字符串 string randomcode = ""; Random rd = new Random(); //生成4位验证码字符串 for (int i = 0; i < 4; i++) randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)]; return randomcode; } //生成验证码图片 protected void Page_Load(object sender, EventArgs e) { //定义图片的宽度 int ImageWidth = 55; //定义图片高度 int ImageHeigh = 22; //定义字体,用于绘制文字 Font font = new Font("Arial", 12, FontStyle.Bold); //定义画笔,用于绘制文字 Brush brush = new SolidBrush(Color.Black); //定义钢笔,用于绘制干扰线 Pen pen1 = new Pen(Color.FromArgb(255, 100, 100), 0);//这里也可以直接获得一个现有的color对象如:Color.Gold.我是为了美观所以定义和下面一样 Pen pen2 = new Pen(Color.FromArgb(255, 100, 100), 0);//这里根据ARGB值定义获得了一个color对象 //创建一个图像 Bitmap BitImage = new Bitmap(ImageWidth, ImageHeigh); //从图像获取一个绘画面 Graphics graphics = Graphics.FromImage(BitImage); //清除整个绘图画面并用颜色填充 graphics.Clear(ColorTranslator.FromHtml("#F0F0F0"));//这里从HTML代码获取color对象 //定义文字的绘制矩形区域 RectangleF rect = new RectangleF(5, 2, ImageWidth, ImageHeigh); //定义一个随机数对象,用于绘制干扰线 Random rand = new Random(); //生成两条横向的干扰线 for (int i = 0; i < 2; i++) { //定义起点 Point p1 = new Point(0, rand.Next(ImageHeigh)); //定义终点 Point p2 = new Point(ImageWidth, rand.Next(ImageHeigh)); //绘制直线 graphics.DrawLine(pen1, p1, p2); } //生成两条纵向的干扰线 for (int i = 0; i < 2; i++) { //定义起点 Point p1 = new Point(rand.Next(ImageWidth), 0); //定义终点 Point p2 = new Point(rand.Next(ImageWidth), ImageHeigh); //绘制直线 graphics.DrawLine(pen2, p1, p2); } //绘制验证码文字 graphics.DrawString(CreateCheckCodeString(), font, brush, rect); //保存图片为gif格式 BitImage.Save(Response.OutputStream, ImageFormat.Gif); //释放对象 graphics.Dispose(); BitImage.Dispose(); }}
[解决办法]
private void Page_Load(object sender, System.EventArgs e) { this.CreateCheckCodeImage(GenerateCheckCode()); }private string GenerateCheckCode() { int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for(int i=0; i<5; i++) { number = random.Next(); if(number % 2 == 0) code = (char)('0' + (char)(number % 10)); else code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode; } private void CreateCheckCodeImage(string checkCode) { if(checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for(int i=0; i<25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 for(int i=0; i<100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
[解决办法]
ASP.NET实现简单的验证码
http://www.dwww.cn/News/2008-2/20082161849376494.shtml