Randomly generated images

Source: Internet
Author: User

Reprinted Source: http://blog.csdn.net/loverszhaokai/archive/2010/10/04/5921139.aspx

//////////////////////////////////////// //////////////////////////////////////// /////////
// If you want to perform verification during login. You only need to determine whether the data entered by the user is equal to the session ["Validate //
//////////////////////////////////////// //////////////////////////////////////// ////////
01. Using system;
02. Using system. drawing;
03. Public partial class: system. Web. UI. Page
04 .{
05. // generate a random image background
06. Protected void createimagem (string validatecode)
07 .{
08. // The image width, which is proportional to the Verification code length
09. Int iwidth = (INT) (validatecode. length * 11.5 );
10. // create an image object with a length of 20 and a width of iwidth
11. system. Drawing. bitmap image = new system. Drawing. Bitmap (iwidth, 20 );
12. // create a new Drawing Object
13. Graphics G = graphics. fromimage (image );
14. // font size and font size for drawing
15. Font F = new system. Drawing. Font ("Arial", 10, system. Drawing. fontstyle. Bold );
16. // the size of the paint brush for drawing
17. Brush B = new system. Drawing. solidbrush (color. White );
18. // clear the background color and fill it with a deep olive color
19. G. Clear (color. darkolivegreen );
20. // format the attributes of the brush and draw images within the specified range with the specified brush, color, etc.
21. G. drawstring (validatecode, F, B, 3, 3 );
22. // create a pencil object
23. Pen blackpen = new pen (color. Black, 0 );
24. // create a random object
25. Random Rand = new random ();
26. // draw a line randomly
27. For (INT I = 0; I <5; I ++)
28 .{
29. Int y = Rand. Next (image. Height );
30. // draw a line with a specified pencil. The width is determined by the parameter.
31. G. drawline (blackpen, 0, Y, image. Width, y );
32 .}
33. // output drawing
34. system. Io. memorystream MS = new system. Io. memorystream ();
35. // Save the image to the specified stream
36. image. Save (MS, system. Drawing. imaging. imageformat. JPEG );
37. response. clearcontent ();
38. // configure the output type
39. response. contenttype = "image/JPEG ";
40. // input content
41. response. binarywrite (Ms. toarray ());
42. // clear unnecessary resources
43. G. Dispose ();
44. image. Dispose ();
45 .}
46. // method for generating random characters
47. Protected string createvalidate (INT count)
48 .{
49. string allchar = "1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 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 ";
50. // save all characters in the verification code in a string array
51. String [] allchararray = allchar. Split (',');
52. // initialize a random number
53. String randomcode = "";
54. Int temp =-1;
55. // generate a random object
56. Random Rand = new random ();
57. // cycle based on the number of digits of the Verification Code
58. For (INT I = 0; I <count; I ++)
59 .{
60. // It mainly prevents the same verification code from being generated
61. If (temp! =-1)
62 .{
63. // Add the time scale
64. Rand = new random (I * temp * (INT) datetime. Now. ticks ));
65 .}
66. Int T = Rand. Next (35 );
67. If (temp = T)
68 .{
69. // regenerate if the values are equal
70. Return createvalidate (count );
71 .}
72. Temp = T;
73. randomcode + = allchararray [T];
74 .}
75. // Save the Random verification code in the session
76. session ["valid"] = randomcode;
77. // return the generated random characters
78. Return randomcode;
79 .}
80. Protected void page_load (Object sender, eventargs E)
81 .{
82. createimagem (createvalidate (8 ));
83 .}
84 .}

 

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.