图书管理图文验证部份

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Security;

public partial class Picture : System.Web.UI.Page
{
    Random rm = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = GetRamdomValidate(4);
        Session["check"] = str;//将验证码写入Session,进行验证,也可以使用cookie
        GetImageValidate(str);
    }
     //得到随机字符串,长度自己定义
   private string GetRamdomValidate(int len)
    {
        int num;
        int tem;
        string rtuStr = "";
        for (int i = 0; i < len; i++)
        {
            num = rm.Next();
        

            //生成数字与字母组合的验证码
            if (num % 2 == 0)
                tem = num % 10 + '0';
            else
                tem = num % 26 + 'A';
                 /*用此方式会生成数字*/
            //  tem = num % 10 + '0';
           
                
            rtuStr +=  Convert.ToChar(tem).ToString();
        }
        return rtuStr;
    }

    //生成图像
   private void GetImageValidate(string strValue)
   {
       //int width = "OO00";
       int width = Convert.ToInt32(strValue.Length * 12);//计算图像宽度
       Bitmap img = new Bitmap(width, 23);
       Graphics gfc = Graphics.FromImage(img); //产生Graphic对象,进行画图
       gfc.Clear(Color.White);
       drawLine(gfc, img);
       //写验证码,需要定义Font
       Font font = new Font("arial", 12, FontStyle.Bold|FontStyle.Italic);
       System.Drawing.Drawing2D.LinearGradientBrush brush =
           new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width,
               img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
       gfc.DrawString(strValue, font, brush, 3, 2);
       drawPoint(img);
       gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);

       //将图像添加到页面
       MemoryStream ms = new MemoryStream();
       img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

       //更改HTTP头
       Response.ClearContent();
       Response.ContentType = "image/gif";
       Response.BinaryWrite(ms.ToArray());
         //Dipose
       gfc.Dispose();
       img.Dispose();
       Response.End();
   }
   private void drawLine(Graphics gfc, Bitmap img)
   {
       for (int i = 0; i < 10; i++)
       {
           int x1 = rm.Next(img.Width);
           int y1 = rm.Next(img.Height);
           int x2 = rm.Next(img.Width);
           int y2 = rm.Next(img.Height);
           gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);//注意画笔一定要浅颜色,否则验证码看不清楚
       }
   }
   private void drawPoint(Bitmap img)
   {
       int col = rm.Next();
       for (int i = 0; i < 100; i++)
       {
           int x = rm.Next(img.Width);
           int y = rm.Next(img.Height);
           img.SetPixel(x, y, Color.FromArgb(col));//Color.FromArgb(rm.Next())生成随机色
       }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值