.net图片验证码

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;
using System.Drawing;

public partial class validate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.GenImg(this.GetCode(4));
}
}
//产生随机字符串
private string GetCode(int num)
{
string[] source={"1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};
string code="";
Random rd=new Random();
for(int i=0;i < num;i++)
{
code += source[rd.Next(0,source.Length)];
}
return code;
}

//生成图片
private void GenImg(string code)
{
Random rd = new Random();
Bitmap myPalette = new Bitmap(120, 60); //定义一个画板
Graphics gh = Graphics.FromImage(myPalette); //在画板上定义绘图的实例
Rectangle rc = new Rectangle(0, 0, 120, 60); //定义一个矩形
String picPath = Server.MapPath("pic/bg" + rd.Next(1,4).ToString().Trim() + ".jpg" );
Bitmap imagefile = (Bitmap)System.Drawing.Image.FromFile(picPath, true); //得到一张位图
TextureBrush texture = new TextureBrush(imagefile); //以图片建立绘图刷
Color[] fontcolor = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Red, Color.Brown, Color.DarkCyan, Color.Purple }; //定义 8 种颜色
String[] fontname = { "Verdana", "System", "Comic Sans MS", "Arial", "宋体" }; //定义 5 种字体
Font myfont; //字体定义
SolidBrush mybrush; //画笔定义


gh.FillRectangle(texture, rc);//使用绘图刷填充矩形,到此得到图片背景

for (short i = 0; i <= code.Length - 1; i++)
{
myfont = new Font(fontname[rd.Next(0, 5)],30,FontStyle.Italic); //随机字体,42号,斜体
mybrush = new SolidBrush(fontcolor[rd.Next(0, 8)]); //随机颜色
gh.DrawString(code.Substring(i,1) , myfont, mybrush, 3 + (i * 23),rd.Next(1,8));//在矩形内画出字符串
}

myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来

Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证

gh.Dispose();
myPalette.Dispose();
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值