asp.net中的captchacode?
我从网站上为验证码编写代码,但当我运行代码时,它工作正常。验证码图像出现了,但我放置了文本框和按钮,但它们不可见。只有验证码图像显示这是代码签出 源代码asp.net中的captchacode?,asp.net,Asp.net,我从网站上为验证码编写代码,但当我运行代码时,它工作正常。验证码图像出现了,但我放置了文本框和按钮,但它们不可见。只有验证码图像显示这是代码签出 源代码 <body> <form id="form1" runat="server"> <IMG height="30" alt="" src="Turing.aspx" style="width :120px;height:60px" /><br /> <br /> &
<body>
<form id="form1" runat="server">
<IMG height="30" alt="" src="Turing.aspx" style="width :120px;height:60px" /><br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Visible ="true" ></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" Visible ="true" />
</form>
</body>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
getImage();
}
private void getImage()
{
Bitmap objBMP = new System.Drawing.Bitmap(150,60);
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
objGraphics.Clear(Color.IndianRed );
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Font objFont = new Font("Arial", 25, FontStyle.Italic );
string randomStr = "";
int[] myIntArray = new int[5];
int x;
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
randomStr += (myIntArray[x].ToString());
}
Session.Add("randomStr", randomStr);
objGraphics.DrawString(randomStr, objFont, Brushes.NavajoWhite, 3, 3);
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["randomStr"].ToString()==TextBox1 .Text .ToString ())
{
Response.Write("yes ok");
}
else
{
Response.Write("Not Inserted");
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
getImage();
}
私有void getImage()
{
位图objBMP=新系统.绘图.位图(150,60);
Graphics objGraphics=System.Drawing.Graphics.FromImage(objBMP);
清晰(颜色为印度红);
objGraphics.TextRenderingHint=TextRenderingHint.AntiAlias;
Font objFont=新字体(“Arial”,25,FontStyle.Italic);
字符串randomStr=“”;
int[]myIntArray=新int[5];
int x;
随机自动与=新随机();
对于(x=0;x<5;x++)
{
myIntArray[x]=System.Convert.ToInt32(autoRand.Next(0,9));
randomStr+=(myIntArray[x].ToString());
}
添加(“randomStr”,randomStr);
拉丝(randomStr,objFont,Brusks.NavajoWhite,3,3);
Response.ContentType=“image/GIF”;
保存(Response.OutputStream,ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
if(会话[“randomStr”].ToString()==TextBox1.Text.ToString())
{
回答。写下(“是-好”);
}
其他的
{
答复。填写(“未插入”);
}
}
但是,当页面执行时,按钮和文本框不可见您正在高举响应并返回生成的图像,而不是带有这行代码的页面html
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
一种可能的解决方法是将映像保存到服务器上的文件系统中,并将映像src设置为指向此位置。然而,这显然会导致大量图像文件,这些文件必须通过后台处理或提交验证码时进行清理
一个更优雅的解决方案是使用HTTPHandler,尽管为了将值保存到会话,您必须确保引用IRequiresessionState。现在,您可以在images source属性中引用此处理程序:
<IMG height="30" alt="" src="CaptchaHandler.ashx" style="width :120px;height:60px" />
[WebService(名称空间=”)]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
公共类CAPTCHANDLER:IHttpHandler,iRequestSessionState
{
公共void ProcessRequest(HttpContext上下文)
{
位图objBMP=新系统.绘图.位图(150,60);
Graphics objGraphics=System.Drawing.Graphics.FromImage(objBMP);
清晰(颜色:印度红);
objGraphics.TextRenderingHint=TextRenderingHint.AntiAlias;
Font objFont=新字体(“Arial”,25,FontStyle.Italic);
字符串randomStr=“”;
int[]myIntArray=新int[5];
int x;
随机自动与=新随机();
对于(x=0;x<5;x++)
{
myIntArray[x]=System.Convert.ToInt32(autoRand.Next(0,9));
randomStr+=(myIntArray[x].ToString());
}
context.Session.Add(“randomStr”,randomStr);
拉丝(randomStr,objFont,Brusks.NavajoWhite,3,3);
context.Response.ContentType=“image/GIF”;
保存(context.Response.OutputStream,ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}
公共布尔可重用
{
得到
{
返回false;
}
}
}
编辑:我的意思是iRequiresessionState不是IReadOnlySessionState,你可以这样做。您正在将图像内容写入输出,替换HTML内容。不要那样,试着用这个。最好使用处理程序(*.ashx)文件而不是ASPX Web表单。同时放置一些随机查询字符串,以便浏览器不会缓存图像
<IMG SRC="yourpage.aspx" />
@Surya sasidhar谢谢:-)
<IMG SRC="yourpage.aspx" />