jpeg没有在服务器上加载Response.Outputstream

时间:2012-09-11 07:23:25

标签: c# asp.net

我正在开发一个ASP.net 2.0 c#项目,我正在创建一个基本的Captcha脚本。 html看起来像这样:

<img height="30" width="80" alt="" src="Captcha.aspx" />

这是Captcha.aspx背后的代码

    protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap objBMP = new System.Drawing.Bitmap(60, 20);
        Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
        objGraphics.Clear( ColorTranslator.FromHtml( "#054196" ) );

        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

        // configure the text
        Font objFont = new Font("Arial", 8, FontStyle.Bold);
        string randomStr = "";
        int[] myIntArray = new int[5];
        int x;

        // randomise the text
        Random autoRand = new Random();

        for (x = 0; x < 5; x++)
        {
            myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
            randomStr += (myIntArray[x].ToString());
        }

        //add string to session
        Session.Add("randomStr", randomStr);

        // draw the text
        objGraphics.DrawString( randomStr, objFont, Brushes.White, 3, 3);

        // Set the content type and return the image
        Response.ContentType = "image/jpeg";
        Encoder quality = Encoder.Quality;
        EncoderParameter qualityParam = new EncoderParameter(quality, 100L);
        EncoderParameters encParams = new EncoderParameters( 1 );
        encParams.Param[0] = qualityParam;
        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
        objBMP.Save(Response.OutputStream, jpgEncoder, encParams);

        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose();
        Response.Flush();
    }


    private ImageCodecInfo GetEncoder(ImageFormat format)
    {

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

这在我的本地计算机上工作正常,但是当上传到我们的开发服务器时它会失败。由于我在项目中的角色,我无法直接访问dev服务器进行调试,所以这是一个试验和错误atm。

任何想法?

1 个答案:

答案 0 :(得分:0)

由于某种原因,服务器不喜欢aspx页面。所以我把验证码移到了一个ashx文件,然后就可以了!