2011-09-22 99 views
0

我對ASP.NET很陌生。客戶端的服務器運行的是.NET 1.1,我想實現此處概述的簡單驗證碼:http://www.codekicks.com/2008/04/implement-simple-captcha-in-cnet.html.NET 1.1 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> 

我收到以下錯誤一旦訪問驗證碼/ 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 

您的幫助表示讚賞。

最佳, 馬克

+0

能上的錯誤(我看不到任何明顯的非C#代碼-1.2不作評論),但是:a).NET 1.1已經過時,b)Web服務器不支持'System.Drawing',並且c)''''語句(跨越對象的生命週期)比'更受歡迎。 Dispose()' –

+0

謝謝Marc。有沒有辦法修改上面的代碼來完成這個在.NET 2.0中沒有修改Web.Config文件?我發現嘗試添加(如使用其他可用解決方案)會導致服務器配置錯誤。因此,動態創建我們的圖像的頁面是可取的。 – garromark

回答

0

試試這一個,而不是從CodeProject上。它非常好 CAPTCHA Image

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>