代码如下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.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
} private string CreateRandomCode(int codeCount)
{
//显示的验证码字符,去掉数字1,大写字母I,数字0,大写字母O
string allChar = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
//**31是上面验证验证码的总数
int t = rand.Next(31);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11);
Bitmap image = new Bitmap(iwidth,15);
Graphics g = Graphics.FromImage(image);
//**生成的字体,字号,样式
Font ft = new Font("Courier New", 10, FontStyle.Regular);
//**产生字体颜色
Brush b = new SolidBrush(Color.DarkGreen);
//**背景颜色
g.Clear(Color.GhostWhite);
g.DrawString(checkCode, ft, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
//**画图片的前景噪音点
for (int i = 0; i < 20; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rand.Next()));
}
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
//**后退也会变
Response.Cache.SetNoStore();
Response.ClearContent();
Response.ContentType = "images/gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
问题1:能让背景透明的吗?
问题2:请怎么让验证码生成一个名为code.gif图片(因为有些空间是不允许Stream的),然后保存在images/下?
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.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
} private string CreateRandomCode(int codeCount)
{
//显示的验证码字符,去掉数字1,大写字母I,数字0,大写字母O
string allChar = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
//**31是上面验证验证码的总数
int t = rand.Next(31);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11);
Bitmap image = new Bitmap(iwidth,15);
Graphics g = Graphics.FromImage(image);
//**生成的字体,字号,样式
Font ft = new Font("Courier New", 10, FontStyle.Regular);
//**产生字体颜色
Brush b = new SolidBrush(Color.DarkGreen);
//**背景颜色
g.Clear(Color.GhostWhite);
g.DrawString(checkCode, ft, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
//**画图片的前景噪音点
for (int i = 0; i < 20; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rand.Next()));
}
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
//**后退也会变
Response.Cache.SetNoStore();
Response.ClearContent();
Response.ContentType = "images/gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
问题1:能让背景透明的吗?
问题2:请怎么让验证码生成一个名为code.gif图片(因为有些空间是不允许Stream的),然后保存在images/下?
建议直接用Response.OutputStream输出。
image.Save(Response.OutputStream, ImageFormat.Gif);
Save方法也有重载的方法可以保存为一个图片,但要对目录有操作的权限。
要用到Session,再把验证码转成字符,然后做对!