.NET 1.1 Captcha实现编译器错误

时间:2011-09-22 06:26:51

标签: c# asp.net .net-1.1

我对ASP.NET很陌生。客户端的服务器正在运行.NET 1.1,我正在尝试实现此处概述的简单验证码:http://www.codekicks.com/2008/04/implement-simple-captcha-in-cnet.html

我在captcha / BuildCaptcha.aspx中有以下代码:

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Color" %>
<%@ Import Namespace="System.Drawing.Imaging" %> 
<script language="C#" runat="server">
Bitmap objBMP = new Bitmap(60, 20);
Graphics objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Wheat);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Italic);
string randomStr = "";
char[] myArray = new char[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
    myArray[x] = System.Convert.ToChar(autoRand.Next(65,90));
    randomStr += (myArray[x].ToString());
}
//This is to add the string to session, to be compared later
Session.Add("RandomStr", randomStr);
//' Write out the text
objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
//' Set the content type and return the image
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
</script>

我在访问captcha / BuildCaptcha.aspx时收到以下错误:

Compiler Error Message: CS1519: Invalid token '(' in class, struct, or interface member declaration

Source Error:

Line 7:  Bitmap objBMP = new Bitmap(60, 20);
Line 8:  Graphics objGraphics = Graphics.FromImage(objBMP);
Line 9:  objGraphics.Clear(Color.Wheat);
Line 10: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Line 11: //' Configure font to use for text


Source File: \\...\captcha\BuildCaptcha.aspx    Line: 9 

感谢您的帮助。

最佳, 标记

2 个答案:

答案 0 :(得分:2)

您应该将此代码放入OnLoad方法中,例如:

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Color" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="C#" runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Bitmap objBMP = new Bitmap(60, 20);
        Graphics objGraphics = Graphics.FromImage(objBMP);
        objGraphics.Clear(Color.Wheat);
        objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        //' Configure font to use for text
        Font objFont = new Font("Arial", 8, FontStyle.Italic);
        string randomStr = "";
        char[] myArray = new char[5];
        int x;
        //That is to create the random # and add it to our string
        Random autoRand = new Random();
        for (x = 0; x < 5; x++)
        {
            myArray[x] = System.Convert.ToChar(autoRand.Next(65, 90));
            randomStr += (myArray[x].ToString());
        }
        //This is to add the string to session, to be compared later
        Session.Add("RandomStr", randomStr);
        //' Write out the text
        objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
        //' Set the content type and return the image
        Response.ContentType = "image/GIF";
        objBMP.Save(Response.OutputStream, ImageFormat.Gif);
        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose();

    }
</script>

答案 1 :(得分:0)

从CodeProject尝试这个。它工作得很好 CAPTCHA Image