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;
public partial class System_CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//RndNum是一个自定义函数
string VNum = RndNum(4); //这里的数字4代表显示的是4位的验证字符串!
Session["VNum"] = VNum;
ValidateCode(VNum);
}
//生成图像函数
private void ValidateCode(string VNum)
{
int Gheight = (int)(VNum.Length * 11.5);
//gheight为图片宽度,根据字符长度自动更改图片宽度
System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight, 18);
Graphics g = Graphics.FromImage(Img);
g.DrawString(VNum, new System.Drawing.Font("Arial", 10), new System.Drawing.SolidBrush(Color.Red), 3, 3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
//生成随机数函数中从Vchar数组中随机抽取
//字母区分大小写
public string RndNum(int VcodeNum)
{
string Vchar = "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,W,X,Y,Z";
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(35) ;
int t = rand.Next(35);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
}
c#验证码
最新推荐文章于 2019-02-02 16:07:03 发布