. Net image Verification Code (C)

Source: Internet
Author: User

1. Create a chkcode. aspx and chkcode. aspx. CS Code as follows:

  1. Using system;
  2. Using system. drawing;
  3. Using system. Drawing. imaging;
  4. Using system. IO;
  5. Public partial class chkcode: system. Web. UI. Page
  6. {
  7. Protected void page_load (Object sender, eventargs E)
  8. {
  9. Showimg ();
  10. }
  11. Private void showimg ()
  12. {
  13. Random ran = new random ();
  14. Int intrandom = ran. Next (1001,999 9 );
  15. // Write a random number (Verification string) to the session
  16. Session. removeall ();
  17. Session ["chkcode"] = intrandom;
  18. // Font name
  19. String strfontname = "Arial ";
  20. // Font size
  21. Int intfontsize = 9;
  22. // Image Width
  23. Int intwidth = 35;
  24. // Image Length
  25. Int intheight = 18;
  26. // Background color
  27. Color bgcolor = colortranslator. fromhtml ("#" + request. querystring ["colorb"]);
  28. // Foreground color
  29. Color forecolor = colortranslator. fromhtml ("#" + request. querystring ["colgrading"]);
  30. // Generate a font
  31. Font forfont = new font (strfontname, intfontsize, fontstyle. Bold );
  32. // Generate an image
  33. Bitmap newbitmap = new Bitmap (intwidth, intheight, pixelformat. format32bppargb );
  34. Graphics G = graphics. fromimage (newbitmap );
  35. // Define the same size of a square box as the word slice
  36. Rectangle newrect = new rectangle (0, 0, intwidth, intheight );
  37. // Apply the background color
  38. G. fillrectangle (New solidbrush (bgcolor), newrect );
  39. // Write
  40. G. drawstring (intrandom. tostring (), forfont, new solidbrush (forecolor), 2, 2 );
  41. Memorystream mstream = new memorystream ();
  42. // Save to memorystream
  43. Newbitmap. Save (mstream, imageformat. GIF );
  44. G. Dispose ();
  45. Newbitmap. Dispose ();
  46. // Send
  47. Response. clearcontent ();
  48. Response. contenttype = "image/GIF ";
  49. Response. binarywrite (mstream. toarray ());
  50. Response. End ();
  51. }
  52. }

2. Call Method

  1. <Asp: Image id = "image1" runat = "server" imageurl = "chkcode. aspx? Colgrading = ff0000 & colorb = efb3ff "/>

 

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.