Simple fabrication of ASP (vb.net+c#)

Source: Internet
Author: User
Tags transparent color
The general method of making verification code effect on website is:
1) Use HttpHandler (general handler) to draw a graph of random verification code, and generate random code, and output to the outputstream of the page.
2) in the page using asynchronous (JS, etc.) to refresh the current page of the verification code.
Example
1) Create a "general application handler ashx" with the following code:
[C #]

public class Validationcode:ihttphandler {//random generator static random r = new Random (Guid.NewGuid (). GetHashCode ()); Exclude black, transparent color, because the background black static propertyinfo[] colors = (typeof (Brushes). GetProperties (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Static)). Where (p = p.name! = "Black" && p.name! = "Transparent"). Select (p = p). ToArray (); Exclude black color, because the undertone black static propertyinfo[] Linecolors = (typeof (Pens). GetProperties (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Static)). Where (p = p.name! = "Black"). Select (p = p). ToArray (); Gets the static class Brushes instance object, static object colorobj = typeof (Brushes). GetConstructor (BindingFlags.NonPublic, NULL, type.emptytypes, NULL); Gets the static class Pens instance object, static object penobj = typeof (Pens). GetConstructor (BindingFlags.NonPublic, NULL, type.emptytypes, NULL); The width of each random character is const float pernumberwidth = 40.0f; Height of each character const FLOat pernumberheight = 50.0f; public void ProcessRequest (HttpContext context) {//Gets the number of random numbers to generate (by default, 5) int reqnum = 5; if (context. request.querystring["Reqnum"]! = null) {int. TryParse (context. request.querystring["Reqnum"], out reqnum); }//Generate how much background map Bitmap BT = new Bitmap ((int) (pernumberwidth*reqnum), (int) pernumberheight); Graphics g = Graphics.fromimage (BT); Generates 4 random numbers (number can be saved to session) string numbers = ""; Draw a number for (int i = 1; I <= reqnum; i++) {numbers + = R.next (0, 9). ToString (); var color = (PropertyInfo) colors. GetValue (r.next (0, colors. Length)); Context. Response.Write (color. Name + "<br/>"); Brush Randomcolor = (brush) color. GetValue (colorobj, NULL); g.DrawString (Numbers[i-1]. ToString (), New Font ("Blackbody", Pernumberwidth), Randomcolor, New PointF ((i-1) *pernumberwidth, 0f)); }//Draw random line int linenum = R.next (10, 21); for (int i = 1; I <= linenum; i++) {var linecolor = (PropertyInfo) linecolors. GetValue (r.next (0, colors. Length)); Pen Randomcolor = (pen) linecolor. GetValue (PenoBJ, NULL); G.drawline (Randomcolor, New PointF (float) (r.nextdouble () * pernumberwidth * reqnum), (float) (r.nextdouble () * Pernumberheight), New PointF ((float) (r.nextdouble () * pernumberwidth * reqnum), (float) (r.nextdouble () * (pernumberheight))); } g.dispose (); Context. Response.Clear (); Context. Response.ContentType = "Image/jpeg"; Bt. Save (context. Response.outputstream, Imageformat.jpeg); Bt. Dispose (); Context. Response.End (); } public bool IsReusable {get {return false;}}}

[vb.net]

Public Class validationcode Implements ihttphandler ' Random generator Shared R as New random (Guid.NewGuid (). GetHashCode ()) ' excludes black, transparent color because the undertone black is Shared colors as PropertyInfo () = (GetType (Brushes). GetProperties (system.reflection.bindingflags.[ Public] or System.Reflection.BindingFlags.GetProperty or System.Reflection.BindingFlags. [Static]). Where (Function (p) p.name <> "Black" AndAlso p.name <> "Transparent"). [Select] (Function (P) p). ToArray () ' excludes black color because the undertone black is Shared linecolors as PropertyInfo () = (GetType (Pens). GetProperties (system.reflection.bindingflags.[ Public] or System.Reflection.BindingFlags.GetProperty or System.Reflection.BindingFlags. [Static]). Where (Function (p) p.name <> "Black"). [Select] (Function (P) p). ToArray () ' Gets the static class Brushes instance object Shared colorobj As Object = GetType (Brushes). GetConstructor (BindingFlags.NonPublic, nothing, type.emptytypes, nothing) ' Get static Class Pens instance object Shared penobj As Object = GetType (Pens). GetConstructor (BindingFlags.NonPublic, nothing, type.emptytypes, Nothing) 'The width of each random character is const pernumberwidth as single = 40F ' height of each character of the const pernumberheight as individual = 50F public Sub ProcessRequest (CO ntext as HttpContext) ' gets how many random numbers to generate (by default 5) Dim Reqnum as Integer = 5 If context. Request.QueryString ("Reqnum") IsNot nothing and then integer.tryparse (context. Request.QueryString ("Reqnum"), Reqnum) End If ' produces how much background map Dim BT as New Bitmap (CInt (math.truncate (pernumberwidth * reqnum) ), CInt (Math.truncate (Pernumberheight))) Dim g As Graphics = Graphics.fromimage (BT) ' generates 4 random numbers (number can be saved to session) Dim Numbers as String = "" ' Draws the number for I as Integer = 1 to reqnum numbers + = R.[next] (0, 9). ToString () Dim color = DirectCast (colors. GetValue (R.[next] (0, colors. Length), PropertyInfo) context. Response.Write (convert.tostring (color. Name) & "<br/>") Dim randomcolor as Brush = DirectCast (color. GetValue (Colorobj, Nothing), Brush) g.drawstring (Numbers (i-1). ToString (), New Font ("Blackbody", Pernumberwidth), Randomcolor, New PointF ((i-1) * pernumberwidth, 0F)) Next ' Draw random lines Dim Linenu M As integer = R.[next] (ten) for I as Integer = 1 to linenum Dim LineColor = DirectCast (linecolors. GetValue (R.[next] (0, colors. Length)), PropertyInfo) Dim randomcolor as Pen = DirectCast (linecolor. GetValue (Penobj, Nothing), Pen) G.drawline (Randomcolor, New PointF (CSng (r.nextdouble () * pernumberwidth * reqnum), CSng ( R.nextdouble () * pernumberheight), New PointF (CSng (r.nextdouble () * pernumberwidth * reqnum), CSng (r.nextdouble () * Pern umberheight)) Next G.dispose () context. Response.Clear () context. Response.ContentType = "Image/jpeg" BT. Save (context. Response.outputstream, Imageformat.jpeg) bt. Dispose () context. Response. [End] () End Sub public ReadOnly Property IsReusable () as a Boolean Get Return False End Get End Property End Class

Attention:
1) Some, such as brushes, require a full list of color attributes through reflection, because they are common, so static variables are used so that they do not have to be initialized every time, saving memory and time.
2) brushes avoid black and transparent color (the background color of this example is black), pens only need to avoid black. For brushes colors, refer to: Http://msdn.microsoft.com/zh-cn/library/system.windows.media.brush (v=vs.95). aspx
3) The bitmap class is used for drawing and is generally a blank black background. Generally mates with the image class +graphics canvas for drawing.
4) The Save method for bitmap has several overloaded versions, one of which can specify an output stream and format the picture. This example is used for this function.
Application

Code in HTML (verification code part, local):


Note that the reason for using JS to set the SRC of IMG is two times because the duplicate path does not raise the request.

Related Article

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.