网站验证码源码(学习)
本代码直接复制进去就可以用了,刷新就可以看到验证码的变动。
这个验证码已经在 DotNet迅网 (http://www.16sw.com) 使用。
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
//myself
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public System.Web.UI.WebControls.Image verifyNumber = new System.Web.UI.WebControls.Image();
protected void Page_Load(object sender, EventArgs e)
{
string tmp = RndNum(Convert.ToInt16(6));
Session["verify"] = tmp;
ValidateCode(tmp);
}
private void ValidateCode(string VNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 9;
Img = new Bitmap(gheight, 18);
g = Graphics.FromImage(Img);
//背景颜色
g.Clear(Color.WhiteSmoke);
//文字字体
Font f = new Font("Tahoma", 9);
//文字颜色
SolidBrush s = new SolidBrush(Color.Red);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum)
{
string MaxNum="";
string MinNum="";
for (int i = 0; i < VcodeNum; i++ )
{
MaxNum = MaxNum + "9";
}
MinNum=MaxNum.Remove(0,1);
Random rd=new Random();
string VNum=Convert.ToString(rd.Next(Convert.ToInt32(MinNum),Convert.ToInt32(MaxNum)));
return VNum;
}
}
using System.Data;
using System.Configuration;
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;
//myself
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public System.Web.UI.WebControls.Image verifyNumber = new System.Web.UI.WebControls.Image();
protected void Page_Load(object sender, EventArgs e)
{
string tmp = RndNum(Convert.ToInt16(6));
Session["verify"] = tmp;
ValidateCode(tmp);
}
private void ValidateCode(string VNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 9;
Img = new Bitmap(gheight, 18);
g = Graphics.FromImage(Img);
//背景颜色
g.Clear(Color.WhiteSmoke);
//文字字体
Font f = new Font("Tahoma", 9);
//文字颜色
SolidBrush s = new SolidBrush(Color.Red);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum)
{
string MaxNum="";
string MinNum="";
for (int i = 0; i < VcodeNum; i++ )
{
MaxNum = MaxNum + "9";
}
MinNum=MaxNum.Remove(0,1);
Random rd=new Random();
string VNum=Convert.ToString(rd.Next(Convert.ToInt32(MinNum),Convert.ToInt32(MaxNum)));
return VNum;
}
}
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |