我用的是vs2010,在一般处理程序中写的代码,下面的代码能生成验证码吗??、using System;
using System.Collections.Generic;
using System.Web;namespace 练习1
{
    /// <summary>
    /// 验证码 的摘要说明
    /// </summary>
    public class YZM : IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/JPEG";
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 50))
            {
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                {
                    Random rand = new Random();
                    int code= rand.Next(1000,9999);
                    string strCode = code.ToString();
                    HttpContext.Current.Session["Code"] = strCode;                       
                    g.DrawString(strCode,new System.Drawing.Font("宋体",12),System.Drawing.Brushes.Red,new System.Drawing.PointF(0,0));
                    bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
                    
                }
            }
        }        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

解决方案 »

  1.   

    上面的处理程序的文件名是YZM.ashx.cs,还有一个是YZM.ashx,YZM.ashx里面的代码是<%@ WebHandler Language="C#" CodeBehind="YZM.ashx.cs" Class="练习1.验证码" %>,现在我要用<img src="##" />实现验证码,但是结果不显示生成的图片
      

  2.   


    用YZM.aspx试试,另外Class能不能不用中文。
      

  3.   

    图片验证码要用ashx一般处理程序写。类型设置为image/JPEG。
      

  4.   

    class用中文对程序没影响,类型设置就是image/JPEG的,但我就是想知道为什么我的代码就实现不了验证码啊、、我想知道有哪些原因
      

  5.   


    <img src="YZM.ashx" />可以生成验证码啊,不知道楼主要干什么。
      

  6.   

    <img src="YZM.ashx" />不能生成的话,自己后台调试下,或者是因为你的Img标签不能显示。