2 qq 35905323 qq_35905323 于 2017.01.04 10:16 提问

这个一般处理程序有什么问题?
 <%@ 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
qq_35905323   2017.01.04 10:23

图片说明

Tiger_Zhao
Tiger_Zhao   Rxr 2017.01.04 10:27

根据错误提示添加类型对应的引用

using System.Drawing;
using System.Drawing.Font;
using System.Windows.Media;
qq_35905323
qq_35905323   2017.01.04 10:45

图片说明
图片说明

showbo
showbo   Ds   Rxr 2017.01.04 11:30

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!