The first reflection is whether these attributes of the verification code can be set (that is, they are encapsulated into a class and used in the form of exposing public attributes and methods, or directly generate the verification code to the output in the general processing program), the result is a pity, it is the latter ...... the algorithm in it feels quite good, at least for someone I don't know about the algorithm, so I thought of encapsulating it and then adding it to my class library shamelessly ^
The old file is as follows: Click to download
First, analyze the shortcomings in this Code (as you think ):
1. process-oriented programming. If you want to modify the attributes of the verification code, such as the font size, background color, and other details, you need to find the relevant code in the general processing program, modified. However, this code is better. you can separate the generated verification code string from the generated image in different ways, so you can easily find the place to modify it.
2. If I want to apply this function to other places, such as the winform program, the Code is not reused as a component (a dll, it is copied and pasted, and then modified...
3. No verification code is found to be saved after it is generated (--!)
Of course, the so-called deficiency is relative, that is, if you have time to think about and then design a verification code class, you can certainly make a relatively good class, however, if you are working on a project and the project is urgent, such code is actually quite powerful. The so-called object-oriented design pattern code decoupling is nothing more than floating cloud ~~~
Next, I will modify the code. First, I will determine what effect I want, that is, how I want to use it after encapsulation. First, I want to use only one new verification code instance (you can set the attributes of the Verification Code as needed ), you can then call some methods of this instance to obtain the verification code in the form of strings, streams, bitmap objects, and byte arrays (Note: This verification code class does not include the function of saving the verification code into the context, I personally think that generating a verification code and saving the verification code to the context are two different functions. It is not necessary to implement this function in the verification code class ). In this case, the code in the general processing program becomes abnormal and simple. First, you only need to generate a new verification code, then call the method to obtain the verification code in the form of a string, and then save it to the previous afternoon, as for the existence of Session or Cookie, the verification code class does not care about this. Then, call another method to obtain the verification code in the form of a byte array, so that the context is used. response. the BinaryWrite () method can output images. That is to say, there are only three codes in the processing program. The calling code is roughly as follows:Copy codeThe Code is as follows: // <summary>
/// Obtain the Verification Code (General handler entry function)
/// </Summary>
/// <Param name = "context"> current context </param>
Public void ProcessRequest (HttpContext context)
{
// Create a verification code
ValidateCode validateCode = new ValidateCode ();
// Obtain the verification code (string) and write it to the Session
Context. Session ["SomeValidateCode"] = validateCode. GetString ();
// Output the verification code (image)
Context. Response. BinaryWrite (validateCode. GetByteArray ());
}
The source code of the verification class (VS2010) is attached here, and the attributes provided in it are not comprehensive. In addition, the image output is output in BMP format, which is much clearer than the original code output, functions are encapsulated to a certain extent but not necessarily well encapsulated for communication and learning: Click to download new
PS: Remember that the code was poorly written (worse than it is now) when I first came out of work. Generally, I used the code directly and never thought about whether to modify or encapsulate the code, at that time, I was deeply impressed by my teacher's instruction. I personally feel that there is nothing bad about using other people's code. Once someone else has used it to prove at least the availability of the Code. In addition, IQ is limited, and some things are not as good as others think, but do not use others' code blindly. It is best to make necessary modifications or encapsulation based on your actual situation, even if it is a simple layer. Of course, it would be a waste of time to do so if the project is urgent. In short, we just don't think about it. If we get it, we will use it, at least let's take a look at the overall structure of the key code or code.
The script house is packed directly. The above is the old folder, and the following is the content of the new folder. /201012/yuanma/yanzhengma.rar