Implementation of an image Verification Code

Source: Internet
Author: User
Tags class generator

In fact, it is not difficult to implement an image verification code. It focuses on how to improve the implementation of time verification.ProgramRecognition difficulty. therefore, in the implementation process, let's take a look at Google's practices. Because numbers or letters overlap properly, the recognition of people is not a problem, but it is relatively more difficult for computing. if it is difficult, you can adjust the program to make the letter closer, or add a curve appropriately to make the analysis more difficult. however, if you have done too much, you may not be clear about it :)

Verification Code Effect

 

Implementation Code
Using system; using system. collections. generic; using system. drawing; using system. drawing. drawing2d; using system. text; namespace Smark. imagenumber {// copyright henryfan 2012 // Email: henryfan@msn.com // homepage: http://www.ikende.com // createtime: 21:18:29 // public class generator {const string value = "123456789"; Private Static dictionary mcharimages = New Dictionary (26); Private Static Image mline; private Static random MRAM = new random (); public static string generatorcode () {string code = ""; for (INT I = 0; I <4; I ++) {code + = value. substring (getramvalue () % value. length, 1);} return code;} static int getramvalue () {return MRAM. next ();} public static image getimage (string code) {bitmap tmpimg = new Bitmap (100, 40); Using (Graphics E = graphics. fromimage (tmpimg) {int offset = 2; E. fillrectangle (New solidbrush (color. white), 0, 0,200, 60); list items = new list (); foreach (char key in code) {items. add (getgeneratoritem (key) ;}for (INT I = 0; I <items. count; I ++) {if (I> 0) {If (items [I]. value <0 & items [I-1]. value> 0) | (items [I]. value> 0 & items [I-1]. value> 0) | (items [I]. value <0 & items [I-1]. value <0) Offset + = 12; else Offset + = 18;} using (image IMG = items [I]. drawimage () {If (math. ABS (items [I]. value)> 20) E. drawimage (IMG, offset, 6); else if (math. ABS (items [I]. value)> 20) E. drawimage (IMG, offset, 4); else E. drawimage (IMG, offset, 2) ;}} return tmpimg;} public static system. collections. ienumerable getkeys {get {return mcharimages. keys ;}} Private Static generatoritem getgeneratoritem (char key) {generatoritem item = new generatoritem (); int value = getramvalue () % 25; If (value <10) value = 10; if (getramvalue () % 2 = 0) value =-value; item. value = value; item. key = key; return item;} class generatoritem {public int value {Get; set;} public char key {Get; set;} public image drawimage () {bitmap tmpimg = new Bitmap (50, 50); Using (Graphics E = graphics. fromimage (tmpimg) {e. rotatetransform (value, system. drawing. drawing2d. matrixorder. append); E. drawimage (mcharimages [Key], 0, 0); E. flush ();} return tmpimg;} static generator () {hatchbrush sb = new hatchbrush (hatchstyle. percent40, color. black, color. black); foreach (char item in value) {bitmap BMP = new Bitmap (50, 50); Using (Graphics G = graphics. fromimage (BMP) {G. drawstring (new string (New char [] {item}), new font ("", 24, fontstyle. italic), Sb, 2, 2);} mcharimages [item] = BMP;} mline = new Bitmap (60, 4); Using (Graphics G = graphics. fromimage (mline) {G. fillrectangle (SB, 0, 0, 60, 3 );}}}}
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.