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.Imaging;
using System.IO;
/// <summary>
/// 引入命名空间
/// 创建生成图片图片验证
/// </summary>public partial class CheckCodeImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//生成的验证码这里该怎么写?这四句是我自己胡乱写的,调试能通过,但是无法在页面显示出图片,
//这四句以外的代码都是书上的例子,应该不会有错,
//现在求教大家,这里该怎么写才能正确显示出图片呢?!
string tmp = CreateCheckCodeString();
HttpCookie a = new HttpCookie("ImageV", tmp);
Response.Cookies.Add(a);
this.CreateCheckCodeImage(tmp); } private string CreateCheckCodeString()
{
//定义拥护验证码的字符数组
char[] allCharArray = { '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'}; //定义验证码字符串
string randomCode = "";
Random rand = new Random(); //生成4位验证码字符串
for (int i = 0; i < 4; i++)
randomCode += allCharArray[rand.Next(allCharArray.Length)];
return randomCode;
} //生成验证码图片
public void CreateCheckCodeImage(string sFilePath)
{
//定义图片宽度
int iWidth = 55;
//定义图片高度
int iHeight = 22;
//定义大小为12pt的 Arial字体,用于绘制文字
Font font = new Font("Arial", 12, FontStyle.Bold);
//定义黑色的单色画笔,用于绘制文字
SolidBrush brush = new SolidBrush(Color.Black);
//定义钢笔,用户绘制干扰线
Pen pen1 = new Pen(Color.Gray, 0); //注意这里直接获得一个现有的Color对象
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100), 0); //注意这里根据Argb值获得了一个Color对象
//创建一个px*20px的图象
Bitmap image = new Bitmap(iWidth, iHeight);
//从图象获得一个绘图面
Graphics g = Graphics.FromImage(image);
//清除整个绘图画面并以指定颜色填充
g.Clear(ColorTranslator.FromHtml("#F0F0F0")); //注意这里从html颜色代码获取Color对象
//定义文字的绘制矩形区域
RectangleF rect = new RectangleF(5, 2, iWidth, iHeight);
//定义一个随机对象,用于绘制干扰线
Random rand = new Random();
//生成两条横向干扰线
for (int i = 0; i < 2; i++)
{
//定义起点
Point p1 = new Point(0, rand.Next(iHeight));
//定义终点
Point p2 = new Point(iWidth, rand.Next(iHeight));
//绘制直线
g.DrawLine(pen1, p1, p2);
}
//生成4条纵向干扰线
for (int i = 0; i < 4; i++)
{
//定义起点
Point p1 = new Point(rand.Next(iHeight), 0);
//定义终点
Point p2 = new Point(rand.Next(iHeight), iHeight);
//绘制直线
g.DrawLine(pen2, p1, p2);
} //绘制验证码文字
g.DrawString(CreateCheckCodeString(), font, brush, rect);
//保存图片为Jpeg格式
image.Save(sFilePath, ImageFormat.Jpeg); //ImageFormat.后面能更改保存图片的格式类型 //释放对象
g.Dispose();
image.Dispose();
}
}
这个参数应该是图片路径,我改成了this.CreateCheckCodeImage(Server.MapPath("aa.jpg")); 结果就有图片了
前台<img src=XX>
这个XX要和你函数中的路径一样
{
//生成的验证码这里该怎么写?这四句是我自己胡乱写的,调试能通过,但是无法在页面显示出图片,
//这四句以外的代码都是书上的例子,应该不会有错,
//现在求教大家,这里该怎么写才能正确显示出图片呢?!
string tmp = CreateCheckCodeString();
HttpCookie a = new HttpCookie("ImageV", tmp);
Response.Cookies.Add(a);
this.CreateCheckCodeImage("a.jpg"); }
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.Imaging;
using System.IO;
namespace test123
{
public partial class WebForm6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//先产生数字串
string checkCode = this.CreateCheckCodeString();
//作图
CreateCheckCodeImage(checkCode);
}
private string CreateCheckCodeString()
{
//定义拥护验证码的字符数组
char[] allCharArray = { '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'}; //定义验证码字符串
string randomCode = "";
Random rand = new Random(); //生成4位验证码字符串
for (int i = 0; i < 4; i++)
randomCode += allCharArray[rand.Next(allCharArray.Length)];
return randomCode;
} //生成验证码图片
public void CreateCheckCodeImage(string checkCode)
{
//定义图片宽度
int iWidth = 55;
//定义图片高度
int iHeight = 22;
//定义大小为12pt的 Arial字体,用于绘制文字
Font font = new Font("Arial", 12, FontStyle.Bold);
//定义黑色的单色画笔,用于绘制文字
SolidBrush brush = new SolidBrush(Color.Black);
//定义钢笔,用户绘制干扰线
Pen pen1 = new Pen(Color.Gray, 0); //注意这里直接获得一个现有的Color对象
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100), 0); //注意这里根据Argb值获得了一个Color对象 //创建一个px*20px的图象
Bitmap image = new Bitmap(iWidth, iHeight);
//从图象获得一个绘图面
Graphics g = Graphics.FromImage(image);
//清除整个绘图画面并以指定颜色填充
g.Clear(ColorTranslator.FromHtml("#F0F0F0")); //注意这里从html颜色代码获取Color对象
//定义文字的绘制矩形区域
RectangleF rect = new RectangleF(5, 2, iWidth, iHeight);
//定义一个随机对象,用于绘制干扰线
Random rand = new Random();
//生成两条横向干扰线
for (int i = 0; i < 2; i++)
{
//定义起点
Point p1 = new Point(0, rand.Next(iHeight));
//定义终点
Point p2 = new Point(iWidth, rand.Next(iHeight));
//绘制直线
g.DrawLine(pen1, p1, p2);
} //生成4条纵向干扰线
for (int i = 0; i < 4; i++)
{
//定义起点
Point p1 = new Point(rand.Next(iHeight), 0);
//定义终点
Point p2 = new Point(rand.Next(iHeight), iHeight);
//绘制直线
g.DrawLine(pen2, p1, p2);
} //绘制验证码文字
g.DrawString(checkCode, font, brush, rect);
//保存图片为Jpeg格式
//image.Save(sFilePath, ImageFormat.Jpeg); //ImageFormat.后面能更改保存图片的格式类型
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
//释放对象
g.Dispose();
image.Dispose();
}
}
}
看红色字体的地方,这样就可以了
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name=".net"></forms>
</authentication>
如果是这样,那么在登陆前,你的显示验证码的页面没有权限访问,所以就出不来了。你试试把它改成<authentication mode="Windows"/>看看行不,如果行,那就把这个验证码页面放在项目以外的地方,或者在这个页面的文件夹下再加个web.config,将这个文件的验证权限设成wondiows。
验证码是出来了
可以怎么验证呢?
一个文本框一个按钮
大哥们指点一下
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.Imaging;
using System.IO;
/// <summary>
/// 引入命名空间
/// 创建生成图片图片验证
/// </summary>public partial class CheckCodeImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//先产生数字串
string checkCode = this.CreateCheckCodeString();
//作图
CreateCheckCodeImage(checkCode);
}
private string CreateCheckCodeString()
{
//定义拥护验证码的字符数组
char[] allCharArray = { 'w','1','2','3','4','5','6','7','8','9','s','m','x',}; //以下为备用数组
//char[] allCharArray = { '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'}; //定义验证码字符串
string randomCode = "";
Random rand = new Random(); //生成4位验证码字符串
for (int i = 0; i < 4; i++)
randomCode += allCharArray[rand.Next(allCharArray.Length)];
return randomCode;
} //生成验证码图片
public void CreateCheckCodeImage(string checkCode)
{
//定义图片宽度
int iWidth = 55;
//定义图片高度
int iHeight = 22;
//定义大小为12pt的 Arial字体,用于绘制文字
Font font = new Font("Arial", 12, FontStyle.Bold);
//定义黑色的单色画笔,用于绘制文字
SolidBrush brush = new SolidBrush(Color.Black);
//定义钢笔,用户绘制干扰线
Pen pen1 = new Pen(Color.Gray, 0); //注意这里直接获得一个现有的Color对象
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100), 0); //注意这里根据Argb值获得了一个Color对象 //创建一个px*20px的图象
Bitmap image = new Bitmap(iWidth, iHeight);
//从图象获得一个绘图面
Graphics g = Graphics.FromImage(image);
//清除整个绘图画面并以指定颜色填充
g.Clear(ColorTranslator.FromHtml("#F0F0F0")); //注意这里从html颜色代码获取Color对象
//定义文字的绘制矩形区域
RectangleF rect = new RectangleF(5, 2, iWidth, iHeight);
//定义一个随机对象,用于绘制干扰线
Random rand = new Random();
//生成两条横向干扰线
for (int i = 0; i < 2; i++)
{
//定义起点
Point p1 = new Point(0, rand.Next(iHeight));
//定义终点
Point p2 = new Point(iWidth, rand.Next(iHeight));
//绘制直线
g.DrawLine(pen1, p1, p2);
} //生成4条纵向干扰线
for (int i = 0; i < 4; i++)
{
//定义起点
Point p1 = new Point(rand.Next(iHeight), 0);
//定义终点
Point p2 = new Point(rand.Next(iHeight), iHeight);
//绘制直线
g.DrawLine(pen2, p1, p2);
} //绘制验证码文字
g.DrawString(checkCode, font, brush, rect);
//保存图片为Jpeg格式
//image.Save(sFilePath, ImageFormat.Jpeg); //ImageFormat.后面能更改保存图片的格式类型
//由这一句取得是生成的验证码的字符串,与页面输入比对匹配验证
HttpContext.Current.Session["ValidateCode"] = checkCode; System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
//释放对象
g.Dispose();
image.Dispose();
}
}然后在需要使用验证的页面,如Login.aspx放入!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Login codeimg</title><script type="text/javascript">
function getImage()
{
var didi = Math.floor(Math.random()*1000);
document.form1.codeimg.src = "CheckCodeImg.aspx?thd ="+didi;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
验证码:将图片中的四个数字填到输入框中
<br />
<asp:TextBox ID="TextBox1" runat="server" Height="22px"></asp:TextBox>
<img alt="ValidateCodeimages" src="CheckCodeImg.aspx" id="codeimg" style="width: 55px; height: 22px" />
<a href ="#" onclick ="getImage()">刷新</a>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" Width="92px" />
</div>
</form>
</body>
</html>
在Login.aspx.cs后台代码中这样写: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;public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
if (Session["ValidateCode"] != null)
{
if (Session["ValidateCode"].ToString() == TextBox1.Text)
{
HttpContext.Current.Response.Write("<script languager='Javascript'>alert('通过验证');</script>");
} else
{
HttpContext.Current.Response.Write("<script languager='Javascript'>alert('请正确输入验证码');</script>"); }
}
}
}这样就完成了!!!
加上后面的代码
//由这一句取得是生成的验证码的字符串,与页面输入比对匹配验证
HttpContext.Current.Session["ValidateCode"] = checkCode; System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
//释放对象
g.Dispose();
image.Dispose();
图片也不显示了
我这里显示不出来
去掉就可以出来了
还有
<script type="text/javascript">
function getImage()
{
var didi = Math.floor(Math.random()*1000);
document.form1.codeimg.src = "CheckCodeImg.aspx?thd ="+didi;
}
</script>
作用是什么啊?没办法验证啊,你看看是不是有这个问题呢?