The learning flow of this article is arranged in this way.
A simple GDI small case
1. Description
If you want to think about how to generate a verification code, then the first problem you have to solve is definitely. NET to generate picture problems dynamically.
Gdi:.net some classes for drawing in a program.
2. Code
1 //Before this code begins, add a reference to the System.Drawing. 2 //Create a memory size of 500*500 3 using (Bitmap bmp = new Bitmap ($)) 4 //Get the picture of the canvas 5 using (Graphics g = Graphics.fromimage (BMP)) 6 {7 //Create Brush 8 using (font font = new Font (FONTFAMILY.GENERICSERIF,)) 9 {10< c10/>//in 100,100 to draw a red HelloWorld11 g.drawstring ("HelloWorld", Font, brushes.red, +,); Draw a blue ellipse at 100 g.drawellipse (Pens.blue, +, +, + ); using stream stream = File.openwrite (@ "d:\2.jpg")) bmp. Save (stream, imageformat.jpeg); }19 }20
Look, it's done. On the code, there seems to be nothing to explain.
Return a picture with a generic handler
1. Description
Next, let's look at how to generate a picture dynamically in a Web page.
From the point of view of the code. There are only a few simple differences with the code on the console above, almost the same!
1) General handlers need to set ContentType = "Image/jpeg" first
2) General processing procedures need to save the picture to Response.outputstream
2. Code
1 context. Response.ContentType = "Image/jpeg"; 2 3 //Before this code begins, add a reference to the System.Drawing. 4 //Create a memory size of 500*500 5 using (Bitmap bmp = new Bitmap ($)) 6 //Get the picture of the canvas 7 using (Graphics g = Graphics.fromimage (BMP)) 8 {9 //Create brush with using (font font = new Font (FONTFAMILY.GENERICSERIF, ten)) { 100,100//Draw a red helloWorld13 g.drawstring ("HelloWorld", Font, brushes.red, +), in 100 , draw a blue ellipse at 100 g.drawellipse (Pens.blue, N, +, +); The image is saved to the output stream, bmp. Save (context. Response.outputstream, imageformat.jpeg); }20
Simple Digital Verification Code
1. Thinking
Now that we've solved the problem of how to dynamically generate pictures, and how to return a picture in a generic handler.
So the technical problem of the verification code, only how to generate a four-bit random number.
In fact, the random number between 1000~9999 is generated using random.
Then put this four-digit number into the session, in accordance with this number to generate a CAPTCHA image.
This is the underlying logic for generating the verification code.
2. Code
1 public void ProcessRequest (HttpContext context) 2 {3 context. Response.ContentType = "Image/jpeg"; 4 5 random random = new Random (), 6 //generates a stochastic number 7 String code = Random. Next (1000, 9999). ToString (); 8 //Put the verification into the session, convenient later than the 9 context. session["Checkcode"] = code;10 //start to generate the verification code of the picture one using (Bitmap bmp = new Bitmap (,)) the using (Graphics g = Graphics.fromimage (BMP)) (font font = new Font (FONTFAMILY.GENERICSERIF)) . DrawString (code, font, Brushes.aliceblue, ten);/ /Picture saved to output stream bmp. Save (context. Response.outputstream, imageformat.jpeg); }20 }
Generate the effect (of course, such a verification code, a little bit of graphics learning can be read by the program, the development of the Do not do so!) )
3. Complications
Originally I knocked on the code to sing this song, quite happy, the result suddenly gave me an empty quoted exception, it is necessary to say a bit.
A slightly more complex verification code
1. Ideas
My idea for improvement is this.
1) Since the random generation of numbers is too simple to feel justified. Then randomly generate 5 characters.
The characters are written in an array. Randomly generates an array subscript, and then takes out five of them. Character array we can write a little more complicated.
2) After the image is generated, throw a bit of mania on the image and increase the recognition cost of other programs.
2. Code
1 public void ProcessRequest (HttpContext context) 2 {3 String checkcode = Gencode (5); Produces a 5-bit random character 4 context. session["Code" = Checkcode; Save the string to the session so that it needs to be validated 5 System.Drawing.Bitmap image = new System.Drawing.Bitmap (70, 22); 6 Graphics g = graphics.fromimage (image); 7 Try 8 {9//Generate random generator stochastic random = new random (); 11//Empty picture background color g.clear (color.white); 13//Draw the background noise line of the picture int i; for (i = 0; i <; i++), and {+ int x1 = random. Next (image. Width); x2 int = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); G.drawline (New Pen (color.silver), x1, y1, x2, y2); Font font = new System.Drawing.Font ("Arial", (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2F, true); g.DrawString (Checkcode, Font, brush, 2, 2); 27//Picture The foreground noise point of the picture G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); System.IO.MemoryStream ms = new System.IO.MemoryStream (); Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); The context. Response.clearcontent (); The context. Response.ContentType = "Image/gif"; The context. Response.BinaryWrite (Ms. ToArray ()); A. {PNS g.dispose (); The image. Dispose (); */<summary> 43//Generate random string from///</summary>//<param name= "Nu M "Random number of characters </param>///<returns> random out string </returns>47 private string gencode (int num) 48 {4 9 string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] chastr = str. ToCharArray (); Wuyi//string[] source ={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "#", "$", "%", "&", "@"}; The String code = ""; Random rd = new random (); the int i; for (i = 0; i < num; i++)//code + + + source[rd. Next (0, source. Length)]; The code + = str. Substring (Rd. Next (0, str. Length), 1); The code; 61}
Create an effect. It's almost ready to use, right?
Okay, today's question about the captcha is that.
I am still that view, this kind of thing does not need to remember, of course, if can really get started to write, it is very good naturally.
Remember not to affect the development, write the time on the Internet to search a little better.
There are two reasons why you should write this article.
1) There is always someone to write this stuff, or where to copy. You can also find yourself after writing an article.
2) No known code, even if it can be copied to use, but the use of it is a guilty conscience.
(Of course, this is not absolutely, too difficult things to forget.) There are so many things that we don't need to know. )
Asp. NET kernel several large objects, ASP. NET Core Knowledge (7)--Reprint