ASP. NET Verification Code (1)

Source: Internet
Author: User
Code
<% @ Page Language = "C #" autoeventwireup = "true" %>
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Configuration" %>
<% @ Import namespace = "system. Collections" %>
<% @ Import namespace = "system. Web" %>
<% @ Import namespace = "system. Web. Security" %>
<% @ Import namespace = "system. Web. UI" %>
<% @ Import namespace = "system. Web. UI. webcontrols" %>
<% @ Import namespace = "system. Web. UI. webcontrols. webparts" %>
<% @ Import namespace = "system. Web. UI. htmlcontrols" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
<% @ Import namespace = "system. Io" %>
<SCRIPT runat = "server">
Private void page_load (Object sender, system. eventargs E)
{
String TMP = rndnum (4 );
Httpcookie A = new httpcookie ("imagev", TMP );
Response. Cookies. Add ();
This. validatecode (TMP );

}
Private void validatecode (string vnum)
{
Bitmap IMG = NULL;
Graphics G = NULL;
Memorystream MS = NULL;

Int gheight = vnum. length * 12;
IMG = new Bitmap (gheight, 25 );
G = graphics. fromimage (IMG );
// Background color
G. Clear (color. lightsteelblue );
// Text font
Font F = new font ("Arial black", 10 );
// Text color
Solidbrush S = new solidbrush (color. royalblue );
G. drawstring (vnum, F, S, 3, 3 );
MS = new memorystream ();
IMG. Save (MS, imageformat. JPEG );
Response. clearcontent ();
Httpcontext. Current. response. contenttype = "image/PNG ";
// Response. contenttype = "images/JPEG ";
Response. binarywrite (Ms. toarray ());
G. Dispose ();
IMG. Dispose ();
Response. End ();
}

Private string rndnum (INT vcodenum)
{
String vchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P "+", Q, R, S, T, U, V, W, X, Y, Z ";
String [] vcarray = vchar. Split (New char [] {','});
String vnum = "";
Int temp =-1;

Random Rand = new random ();

For (INT I = 1; I <vcodenum + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}

Int T = Rand. Next (35 );
If (temp! =-1 & temp = T)
{
Return rndnum (vcodenum );
}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum;
}

</SCRIPT>
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.