qq_35905323 2017-01-04 02:16 采纳率: 14.8%
浏览 883

这个一般处理程序有什么问题?

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

using System;
using System.Web;

public class ValidCode : IHttpHandler {

    public void ProcessRequest (HttpContext context) 
    {
        string strValidCode = GetValidCode(5);
        context.Session["ValidCode"] = strValidCode;
        Bitmap image = new Bitmap(120, 30);
        Graphics g = Graphics.FromImage(image);
        try
        {
            Random random = new Random();
            g.Clear(Color.White);
            for(int i=0;i<25;i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }
            Font font = new System.Drawing.Font("Arial", 20, (System.Drawing.FontStyle.Bold));
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.DarkRed, 3, true);
            g.DrawString(strValidCode, font, brush, 5, 2);
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Heigth - 1);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, ImageFormat.Gif);
              context.Response.ClearContent();
              context.Response.ContentType = "image/Gif";
              context.Response.BinaryWrite(ms.ToArray());
    }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
 }
    private string GetValidCode(int num)
    {
        string strRandomCode = "ABCD1EF2GH3IJ4KL5MN6P7QR8ST9UVWXYZ";
        char[] chastr = strRandomCode.ToCharArray();
        StringBuilder sbValidCode = new StringBuilder();
        Random rd = new Random();
        for(int i=0;i<num;i++)
        {
            sbValidCode.Append(strRandomCode.Substring(rd.Next(0, strRandomCode.Length), 1));
        }
        return sbValidCode.ToString();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}
  • 写回答

4条回答 默认 最新

  • qq_35905323 2017-01-04 02:23
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘