开发者

captchacode in asp.net?

开发者 https://www.devze.com 2022-12-16 06:37 出处:网络
i write the code for captcha from the website, but when i run the code it is working fine captcha image is coming but i place the textbox and a button but they are not visible only capctch image is di

i write the code for captcha from the website, but when i run the code it is working fine captcha image is coming but i place the textbox and a button but they are not visible only capctch image is displaying this is code check out

source code

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

Code behind:

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");
    }
}

but button and textbox is not visible when page executed


You are highjacking the response and returning your generated image instead of the pages html with this line of code.

    objBMP.Save(Response.OutputStream, ImageFormat.Gif);

A possible way around this is to instead save the image to the filesystem on the server and set the image src to point at this. However this will obviously result in a lot of image files which will have to be cleaned up with either a background process or when the captcha is submitted.

A more elegant solution would be to use a HTTPHandler although you must ensure you reference IRequiresSessionState in order to save your value to session. You can now refer to this handler in the images source property:

<IMG height="30" alt="" src="CaptchaHandler.ashx"  style="width :120px;height:60px" />

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class CaptchaHandler : IHttpHandler, IRequiresSessionState 
{

    public void ProcessRequest(HttpContext context)
    {
        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());
        }

        context.Session.Add("randomStr", randomStr);

        objGraphics.DrawString(randomStr, objFont, Brushes.NavajoWhite, 3, 3);
        context.Response.ContentType = "image/GIF";
        objBMP.Save(context.Response.OutputStream, ImageFormat.Gif);
        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose(); 

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Edit: I meant IRequiresSessionState not IReadOnlySessionState


You can do this. You are writing the Image content as Output, replacing the HTML Content. Instead of that try using this. It is better to use Handler (*.ashx) files instead of ASPX Web Forms. Also put some random querystring so that browsers will not cache the Image.

<IMG SRC="yourpage.aspx" />
0

精彩评论

暂无评论...
验证码 换一张
取 消