C#实现验证码识别

转 http://blog.csdn.net/zgke/article/details/4170956

这个可能是最简单的严整码了..

 

验证

 

 

使用

 

 System.Net.WebClient _Client =new System.Net.WebClient();
            byte[] _ImageBytes =_Client.DownloadData("http://www.fjjj.gov.cn/Article/getcode.asp");

            MemoryStream _Memory = new MemoryStream(_ImageBytes);
            Bitmap _OldBitmap = (Bitmap)Image.FromStream(_Memory);
            pictureBox1.Image = _OldBitmap;

            this.Text = GetCodeText(_OldBitmap);

 

全部代码

 

 

[c-sharp] view plain copy print ?
  1.    /// <summary>   
  2.         /// 获取验证码   
  3.         /// </summary>   
  4.         /// <param name="p_Bitmap">图形 http://www.fjjj.gov.cn/Article/getcode.asp</param>   
  5.         /// <returns>数值</returns>   
  6.         public static string GetCodeText(Bitmap p_Bitmap)  
  7.         {  
  8.             int _Width = p_Bitmap.Width / 4;  
  9.             int _Height = p_Bitmap.Height;  
  10.   
  11.             Bitmap[] _Bitmap = new Bitmap[4];  
  12.             Rectangle _Rectangle = new Rectangle();  
  13.             _Rectangle.Width = _Width;  
  14.             _Rectangle.Height = _Height;  
  15.             for (int i = 0; i != _Bitmap.Length; i++)  
  16.             {  
  17.                 _Bitmap[i] = p_Bitmap.Clone(_Rectangle, p_Bitmap.PixelFormat);  
  18.                 _Rectangle.X += _Width;  
  19.             }  
  20.             int _Value1 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[0]));  
  21.             int _Value2 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[1]));  
  22.             int _Value3 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[2]));  
  23.             int _Value4 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[3]));  
  24.   
  25.             string _Value = _Value1 == -1 ? "?" : _Value1.ToString();  
  26.              _Value += _Value2 == -1 ? "?" : _Value2.ToString();  
  27.              _Value += _Value3 == -1 ? "?" : _Value3.ToString();  
  28.              _Value += _Value4 == -1 ? "?" : _Value4.ToString();  
  29.              return _Value;   
  30.   
  31.         }  
  32.            
  33.   
  34.         private static string[] _TextBytes = new string[]  
  35. {  
  36.     "E17BEFBDF7DE7BEFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  37.     "FBE3BFFFFEFBEFBFFFFEFB83FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  38.     "E17BFFFDF7EFDFBF7FFFFE03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  39.     "E17BFFFDF7E37FFFFDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  40.     "EF9FBFFEFAEDBB0FFCFBEF1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  41.     "C0FBEFBFFFE07FFFFDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  42.     "E3F7EFBFFFE273EFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  43.     "C07BFFFEFBF7DFBFFFFEFDF7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  44.     "E17BEFBDF7E17BEFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",  
  45.     "E17BEFBDF7CE47FFFDF7EFC7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"      
  46. };  
  47.   
  48.   
  49.         /// <summary>   
  50.         /// 获取二值化数据   
  51.         /// </summary>   
  52.         /// <param name="p_Bitmap">图形</param>   
  53.         /// <returns>二值化数据</returns>   
  54.         public static string GetImageText(Bitmap p_Bitmap)  
  55.         {  
  56.             int _Width = p_Bitmap.Width;  
  57.             int _Height = p_Bitmap.Height;  
  58.             BitmapData _Data = p_Bitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadOnly, p_Bitmap.PixelFormat);  
  59.   
  60.             byte[] _DataByte = new byte[_Data.Stride * _Height];  
  61.   
  62.             Marshal.Copy(_Data.Scan0, _DataByte, 0, _DataByte.Length);  
  63.   
  64.             BitArray _Bitarray = new BitArray(_DataByte.Length, true);  
  65.   
  66.             int _Index = 0;  
  67.             for (int i = 0; i != _Height; i++)  
  68.             {  
  69.                 int _WidthStar = i * _Data.Stride;  
  70.                 for (int z = 0; z != _Width; z++)  
  71.                 {  
  72.                     if (_DataByte[_WidthStar + (z * 3)] == 238 && _DataByte[_WidthStar + (z * 3) + 1] == 238 && _DataByte[_WidthStar + (z * 3) + 2] == 238)  
  73.                     {  
  74.                         _Bitarray[_Index] = true;  
  75.                     }  
  76.                     else  
  77.                     {  
  78.                         _Bitarray[_Index] = false;  
  79.                     }  
  80.                     _Index++;  
  81.                 }  
  82.             }  
  83.             p_Bitmap.UnlockBits(_Data);  
  84.   
  85.             int _ByteIndex =_Bitarray.Count / 8;  
  86.             if (_Bitarray.Count % 8 != 0) _ByteIndex++;  
  87.   
  88.             byte[] _Temp =new byte[_ByteIndex];  
  89.             _Bitarray.CopyTo(_Temp, 0);  
  90.   
  91.             return BitConverter.ToString(_Temp).Replace("-""");  
  92.         }  

/// <summary> /// 获取验证码 /// </summary> /// <param name="p_Bitmap">图形 http://www.fjjj.gov.cn/Article/getcode.asp</param> /// <returns>数值</returns> public static string GetCodeText(Bitmap p_Bitmap) { int _Width = p_Bitmap.Width / 4; int _Height = p_Bitmap.Height; Bitmap[] _Bitmap = new Bitmap[4]; Rectangle _Rectangle = new Rectangle(); _Rectangle.Width = _Width; _Rectangle.Height = _Height; for (int i = 0; i != _Bitmap.Length; i++) { _Bitmap[i] = p_Bitmap.Clone(_Rectangle, p_Bitmap.PixelFormat); _Rectangle.X += _Width; } int _Value1 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[0])); int _Value2 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[1])); int _Value3 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[2])); int _Value4 = Array.IndexOf(_TextBytes, GetImageText(_Bitmap[3])); string _Value = _Value1 == -1 ? "?" : _Value1.ToString(); _Value += _Value2 == -1 ? "?" : _Value2.ToString(); _Value += _Value3 == -1 ? "?" : _Value3.ToString(); _Value += _Value4 == -1 ? "?" : _Value4.ToString(); return _Value; } private static string[] _TextBytes = new string[] { "E17BEFBDF7DE7BEFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FBE3BFFFFEFBEFBFFFFEFB83FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "E17BFFFDF7EFDFBF7FFFFE03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "E17BFFFDF7E37FFFFDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "EF9FBFFEFAEDBB0FFCFBEF1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C0FBEFBFFFE07FFFFDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "E3F7EFBFFFE273EFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C07BFFFEFBF7DFBFFFFEFDF7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "E17BEFBDF7E17BEFBDF7DE87FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "E17BEFBDF7CE47FFFDF7EFC7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" }; /// <summary> /// 获取二值化数据 /// </summary> /// <param name="p_Bitmap">图形</param> /// <returns>二值化数据</returns> public static string GetImageText(Bitmap p_Bitmap) { int _Width = p_Bitmap.Width; int _Height = p_Bitmap.Height; BitmapData _Data = p_Bitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadOnly, p_Bitmap.PixelFormat); byte[] _DataByte = new byte[_Data.Stride * _Height]; Marshal.Copy(_Data.Scan0, _DataByte, 0, _DataByte.Length); BitArray _Bitarray = new BitArray(_DataByte.Length, true); int _Index = 0; for (int i = 0; i != _Height; i++) { int _WidthStar = i * _Data.Stride; for (int z = 0; z != _Width; z++) { if (_DataByte[_WidthStar + (z * 3)] == 238 && _DataByte[_WidthStar + (z * 3) + 1] == 238 && _DataByte[_WidthStar + (z * 3) + 2] == 238) { _Bitarray[_Index] = true; } else { _Bitarray[_Index] = false; } _Index++; } } p_Bitmap.UnlockBits(_Data); int _ByteIndex =_Bitarray.Count / 8; if (_Bitarray.Count % 8 != 0) _ByteIndex++; byte[] _Temp =new byte[_ByteIndex]; _Bitarray.CopyTo(_Temp, 0); return BitConverter.ToString(_Temp).Replace("-", ""); }

 

 

 

 

你可能感兴趣的:(C#实现验证码识别)