空心字体验证码

<%@ WebHandler Language="C#" Class="Handler" %>


using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Web.SessionState;
using System.Drawing.Text;
using System.Drawing.Drawing2D;


public class Handler : IHttpHandler, IRequiresSessionState
{


    static PrivateFontCollection pfc = null;
    public void ProcessRequest(HttpContext context)
    {
        string checkCode = context.Request.QueryString["ValidateCode"];
        if (checkCode == null)
            checkCode = "0 0 0 0";
        context.Session["GetCode"] = checkCode;
        int iwidth = 108;//(int)(checkCode.Length * 11.5);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 45);
        Graphics g = Graphics.FromImage(image);
        Random random = new Random(System.DateTime.Now.Millisecond);
        
        


        
        //Font f = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular);


        //Brush b = new System.Drawing.SolidBrush(Color.Black);
        Brush b = new System.Drawing.SolidBrush(Color.FromArgb(4683611));
        //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
        //Color color = Color.FromArgb(16775149);
        g.Clear(Color.FromArgb(237, 247, 255));
        using (StringFormat format = new StringFormat())
        {
            
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            format.FormatFlags = StringFormatFlags.NoWrap;
            Matrix matrix = new Matrix();
            float offsetx = -35, offsety = 0;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            for (int i = 0; i < checkCode.Length; i++)
            {
                int fsize = random.Next(24,28);
                Font f = CreateFont(context.Server.MapPath("tt0767m_.TTF"), fsize, FontStyle.Bold, GraphicsUnit.Point, 0);
                SizeF size = g.MeasureString(checkCode[i].ToString(), f);

                matrix.RotateAt(random.Next(-15, 10), new PointF(offsetx+size.Width/2, offsety+size.Height/2));
                g.Transform = matrix;



                g.DrawString(
                             checkCode[i].ToString(),
                             f,
                             Brushes.Green,
                             new RectangleF(
                             offsetx,
                             offsety,
                             image.Width,
                             image.Height),
                            format);            
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值