页面ValidateImage.aspx 用来创建显示验证码字符串的图片,并在该图片上添加验证码字符串

本文介绍了一种通过随机设置字体样式和画刷属性来提高验证码辨识难度的方法。具体包括生成随机字体样式的函数、初始化画刷列表的过程以及创建并显示验证码图片的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//为了使得验证码字符串显得难以辨认,该系统在输出验证码字符串时,随机设置了字符串的FontStyle属性和Brush属性。

//根据随机数random产生获取FontStyle属性的枚举值(一个整数)
private int CreateRandomFontStyle(int random)
{
  if(random<200){return 0;}
  if(random<400){return 1;}
  if(random<600){return 2;}
  if(random<800){return 3;}
  if(random<1000){return 4;}
  return 4;
}


//函数InitBrushList()初始化保存Brush对象的数组BrushList,即把系统预先设置的Brush枚举添加到数组BrushList中。
private void IntBrushList()
{
  BrushList[0]=SystemBrushes.ActiveBorder;
   ............
  BrushList[0]=SystemBrushes.WindowText;  
}


//页面创建并显示验证码字符串的图片的步骤如下
(1)函数首先调用函数InitBrushList()初始化保存Brush对象的数组BrushList;
(2)获取服务器端产生的验证码字符串;
(3)创建一个BMP位图;
(4)在BMP位图上输出验证码字符串;
(5)设置输出格式,并输出该页面


  private readonly string ImagePath="Images/Validator.jpg";
  private string sValidator="";
  private Brush[] BrushList=new Brush[32];
private void Page_Load(object sender,System.EventArgs e)
{
   InitBrushList();
   if(Request.Params["Validator"]!=null)
    {//获取验证字符串
      sValidator=Request.Params["Validator"].ToString();
    }
   //创建Bmp位图
   Bitmap bitMapImage=new System.Drawing.Bitmap(Server.MapPath(ImagePath));
   Graphics graphicImage=Graphics.FromImage(bitMapImage);
   //设置画笔的输出模式
   graphicImage.SmoothingMode=SmoothingMode.AntiAlias;
   //添加文本字符串
   for(int i=0;i<sValidator.Length;i++)
   {
      graphicImage.DrawString(sValidator[i].ToString(),new Font("Arial",20,(FontStyle)CreateRandomFontStyle(GetRandomint(0,1000))),BrushList[GetRandomint(0,BrushList.Length-1)],new PointF(i*15,GetRandomint(-5,5)));
   }
   //设置图像输出的格式
   Response.ContentType="image/jpeg";
   //保存数据流
   bitMapImage.Save(Response.OutputStream,ImageFormat.Jpeg);
   //释放占有的资源
  graphicImage.Dispose();
  bitMapImage.Dispose();
  Response.End();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值