.NET 1.1 Captcha Implementation Compiler Error
I'm quite new to ASP.NET. A client's server is running .NET 1.1 and I am trying to implement the simple captcha outlined here: http://www.codekicks.com/2008/04/implement-simple-captcha-in-cnet.html
I have the following code in captcha/BuildCaptcha.aspx:
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Color" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
&l开发者_Python百科t;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>
I am receiving the following error upon visiting 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
Your help is appreciated.
Best, Mark
You should put this codes into a method, OnLoad for example:
<%@ 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>
Try this one instead from CodeProject. It works very well CAPTCHA Image
精彩评论