首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET 1.1验证码实现编译器错误

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

Stack Overflow用户
提问于 2011-09-22 14:26:51
回答 2查看 407关注 0票数 0

我对ASP.NET非常陌生。客户端服务器运行的是.NET 1.1,我正在尝试实现这里列出的简单验证码:http://www.codekicks.com/2008/04/implement-simple-captcha-in-cnet.html

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

代码语言:javascript
复制
<%@ 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时收到以下错误:

代码语言:javascript
复制
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 

非常感谢您的帮助。

最好的,马克

EN

回答 2

Stack Overflow用户

发布于 2011-09-22 14:50:04

您应该将这些代码放入一个方法中,例如OnLoad:

代码语言:javascript
复制
<%@ 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>
票数 2
EN

Stack Overflow用户

发布于 2011-09-22 14:48:38

试试这个,而不是CodeProject。它工作得非常好,CAPTCHA Image

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7510444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档