. net 1.1验证码实现编译器错误

本文关键字:编译器 错误 实现 验证 net | 更新日期: 2023-09-27 18:09:26

我是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 

谢谢你的帮助。

最好的,div标记

您应该将这些代码放入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>

. net 1.1验证码实现编译器错误

试试这个代码项目。效果很好验证码图片