C#實現驗證碼識別例項
驗證碼例項如下,形同動網論壇所採用的驗證碼:
測試: System.Net.WebClient _Client =new System.Net.WebClient();
byte[] _ImageBytes =_Client.DownloadData(");
MemoryStream _Memory = new MemoryStream(_ImageBytes);
Bitmap _OldBitmap = (Bitmap)Image.FromStream(_Memory);
pictureBox1.Image = _OldBitmap;
this.Text = GetCodeText(_OldBitmap);
實現程式碼如下: ///
/// 獲取驗證碼
///
/// 圖形
/// 數值
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"
};
///
/// 獲取二值化資料
///
/// 圖形
/// 二值化資料
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("-", "");
}
///
/// 獲取驗證碼
///
/// 圖形
/// 數值
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"
};
///
/// 獲取二值化資料
///
/// 圖形
/// 二值化資料
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("-", "");
}
[@more@]
byte[] _ImageBytes =_Client.DownloadData(");
MemoryStream _Memory = new MemoryStream(_ImageBytes);
Bitmap _OldBitmap = (Bitmap)Image.FromStream(_Memory);
pictureBox1.Image = _OldBitmap;
this.Text = GetCodeText(_OldBitmap);
/// 獲取驗證碼
///
/// 圖形
///
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"
};
///
/// 獲取二值化資料
///
/// 圖形
///
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("-", "");
}
///
/// 獲取驗證碼
///
/// 圖形
///
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"
};
///
/// 獲取二值化資料
///
/// 圖形
///
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("-", "");
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16396910/viewspace-1032155/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python驗證碼識別處理例項Python
- javascript實現的驗證碼程式碼例項JavaScript
- canvas實現的驗證碼效果程式碼例項Canvas
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- javascript實現的簡單驗證碼效果程式碼例項JavaScript
- 使用 C# 識別英文數字驗證碼C#
- 使用 Chapel 實現滑動驗證碼識別
- 驗證碼識別
- javascript實現的生成驗證碼隨機數程式碼例項JavaScript隨機
- 精準實現身份證號碼格式校驗程式碼例項
- QQ驗證碼識別原始碼(C#/NET1.1)原始碼C#
- jQuery實現的表單註冊驗證程式碼例項jQuery
- 使用 Seed7 實現滑動驗證碼識別
- 使用 ActionScript 實現簡單滑動驗證碼識別
- 初探驗證碼識別
- java識別驗證碼Java
- jquery 驗證碼效果程式碼例項jQuery
- python 驗證碼識別示例(一) 某個網站驗證碼識別Python網站
- Asp.net(C#)實現驗證碼功能ASP.NETC#
- jQuery加法驗證碼效果程式碼例項jQuery
- Python驗證碼識別:利用pytesser識別簡單圖形驗證碼Python
- 使用 Forth 實現驗證碼識別與自動化登入
- 使用 Crystal 實現驗證碼識別與自動化登入
- JavaScript 表單驗證程式碼例項JavaScript
- 郵箱格式驗證程式碼例項
- 實時驗證碼技術可改進生物識別身份驗證
- ajax實現的驗證使用者名稱是否存在程式碼例項
- 影片直播app原始碼,傳送驗證碼 驗證碼識別APP原始碼
- 有了這個Python庫,免費實現驗證碼識別!Python
- 使用 C++ 實現驗證碼識別與自動化登入C++
- 智慧實驗室-通用認證碼識別(Captchio) 2.0.0.80APT
- javascript驗證郵箱格式程式碼例項JavaScript
- Python生成驗證碼例項講解Python
- C#實現單例項執行C#單例
- 驗證手機號碼格式的程式碼例項
- C#滑動拼圖驗證碼實現筆記C#筆記
- 字元型圖片驗證碼識別完整過程及Python實現字元Python
- js驗證表單項是否為空例項程式碼JS