验证码

<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

public class MyHandler : IHttpHandler, IRequiresSessionState {   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/gif";
        Bitmap basemap = new Bitmap(150, 45);
        Graphics graph = Graphics.FromImage(basemap);
        graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 150, 45);
        Font font = new Font(FontFamily.GenericSerif, 38, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();
        string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789abcdefghijklmnpqrstuvwxyz";
        string letter = string.Empty;
        StringBuilder sb = new StringBuilder();
        Color c1 = Color.FromArgb(17, 129, 190);
        Color c2 = Color.FromArgb(115, 17, 190);
        Color c3 = Color.FromArgb(3, 106, 76);
        Color c4 = Color.FromArgb(98, 80, 15);
        Color c5 = Color.FromArgb(94, 8, 102);
        Color[] color = new Color[] { c1, c2, c3, c4, c5 };
        for (int x = 0; x < 5; x++)
        {
            letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
            sb.Append(letter);
            graph.DrawString(letter, font, new SolidBrush(color[r.Next(0, 4)]), x * 26, r.Next(0, 3));
        }     
        for (int x = 0; x < 3; x++)
        {
            Pen linePen = new Pen(new SolidBrush(color[r.Next(0, 4)]), 2);
            graph.DrawLine(linePen, new Point(r.Next(0, 149), r.Next(0, 44)), new Point(r.Next(0, 149), r.Next(0, 44)));
        }  
        basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
        graph.Dispose();
        basemap.Dispose();
        context.Session["CheckCode"] = sb.ToString();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值