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);
/// <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("-", ""); }
数字
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace CrazyCoder.Common.Decode { public class GanjiImageDecode : CrazyCoder.Common.Decode.ImageDeCodeBase { public GanjiImageDecode(Bitmap pic) : base(pic) { } public GanjiImageDecode(string path) : base(path) { } //特征码数组 string[] CodeArray = new string[] { "00011111000001111111000111000111001100000110110000000111100000001111000000011110000000111100000001101100000110011100011100011111110000011111000",//0 "0111111110110011001100110011001100110011001100110011",//1 "001111100011111110110000111000000011000000011000000110000001110000111000001110000011000000110000000111111111111111111",//2 "001111100011111110110000111000000011000000011000000111000011110000011110000000111000000011110000111011111110001111100",//3 "00000011100000000111000000011110000001101100000110011000011000110001100001100111000011001111111111111111111111000000011000000000110000000001100",//4 "111111110111111110110000000110000000110000000111111000000001110000000111000000011000000011010000111111111110011111100",//5 "0001111000001111111001110001000110000000110000000011001111001111111110111000011111000000110110000011011100011100111111100001111100",//6 "11111111111111110000011000001100000011000001100000110000001100000010000001100000011000000110000001100000",//7 "001111100011111110111000111110000011111000111011111110011111110111000111110000011110000011111000111011111110001111100",//8 "0011111000011111110011100011101100000110110000011111100011110111111011001111001100000000110000000110001000111001111111000011111000",//9 "11111111"//- }; public Bitmap[] GetPic() { ToGrayByPixels(); //灰度处理 Bitmap bmp = GetPicValidByValue(128); Bitmap[] pics = GetSplitPics2(bmp,128); //分割 for (int i = 0; i < pics.Length; i++) { if (pics[i] != null) { Bitmap b = pics[i]; pics[i] = GetPicValidByValue(pics[i], 128); b.Dispose(); } } return pics; } public string GetCodeString() { Bitmap[] pics = GetPic(); string numStr = ""; for (int i = 0; i < pics.Length; i++) { string str = GanjiImageDecode.GetSingleBmpCode(pics[i], 128); for (int j = 0; j < CodeArray.Length; j++) { if (CodeArray[j] == str) { if (j < CodeArray.Length - 1) { numStr += j.ToString(); } else { numStr += "-"; } } } } return numStr; } } }
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace BallotAiying2 { class UnCodebase { public Bitmap bmpobj; public UnCodebase(Bitmap pic) { bmpobj = new Bitmap(pic); //转换为Format32bppRgb } /// <summary> /// 根据RGB,计算灰度值 /// </summary> /// <param name="posClr">Color值</param> /// <returns>灰度值,整型</returns> private int GetGrayNumColor(System.Drawing.Color posClr) { return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; } /// <summary> /// 灰度转换,逐点方式 /// </summary> public void GrayByPixels() { for (int i = 0; i < bmpobj.Height; i++) { for (int j = 0; j < bmpobj.Width; j++) { int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i)); bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue)); } } } /// <summary> /// 去图形边框 /// </summary> /// <param name="borderWidth"></param> public void ClearPicBorder(int borderWidth) { for (int i = 0; i < bmpobj.Height; i++) { for (int j = 0; j < bmpobj.Width; j++) { if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth) bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255)); } } } /// <summary> /// 灰度转换,逐行方式 /// </summary> public void GrayByLine() { Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height); BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb); // bmpData.PixelFormat = PixelFormat.Format24bppRgb; IntPtr scan0 = bmpData.Scan0; int len = bmpobj.Width * bmpobj.Height; int[] pixels = new int[len]; Marshal.Copy(scan0, pixels, 0, len); //对图片进行处理 int GrayValue = 0; for (int i = 0; i < len; i++) { GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i])); pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte } bmpobj.UnlockBits(bmpData); } /// <summary> /// 得到有效图形并调整为可平均分割的大小 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public void GetPicValidByValue(int dgGrayValue, int CharsCount) { int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; int posx2 = 0; int posy2 = 0; for (int i = 0; i < bmpobj.Height; i++) //找有效区 { for (int j = 0; j < bmpobj.Width; j++) { int pixelValue = bmpobj.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; // 确保能整除 int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数 if (Span < CharsCount) { int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1 if (posx1 > leftSpan) posx1 = posx1 - leftSpan; if (posx2 + Span - leftSpan < bmpobj.Width) posx2 = posx2 + Span - leftSpan; } //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); } /// <summary> /// 得到有效图形,图形为类变量 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public void GetPicValidByValue(int dgGrayValue) { int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; int posx2 = 0; int posy2 = 0; for (int i = 0; i < bmpobj.Height; i++) //找有效区 { for (int j = 0; j < bmpobj.Width; j++) { int pixelValue = bmpobj.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); } /// <summary> /// 得到有效图形,图形由外面传入 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue) { int posx1 = singlepic.Width; int posy1 = singlepic.Height; int posx2 = 0; int posy2 = 0; for (int i = 0; i < singlepic.Height; i++) //找有效区 { for (int j = 0; j < singlepic.Width; j++) { int pixelValue = singlepic.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); return singlepic.Clone(cloneRect, singlepic.PixelFormat); } /// <summary> /// 平均分割图片 /// </summary> /// <param name="RowNum">水平上分割数</param> /// <param name="ColNum">垂直上分割数</param> /// <returns>分割好的图片数组</returns> public Bitmap [] GetSplitPics(int RowNum,int ColNum) { if (RowNum == 0 || ColNum == 0) return null; int singW = bmpobj.Width / RowNum; int singH = bmpobj.Height / ColNum; Bitmap [] PicArray=new Bitmap[RowNum*ColNum]; Rectangle cloneRect; for (int i = 0; i < ColNum; i++) //找有效区 { for (int j = 0; j < RowNum; j++) { cloneRect = new Rectangle(j*singW, i*singH, singW , singH); PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图 } } return PicArray; } /// <summary> /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景 /// </summary> /// <param name="singlepic">灰度图</param> /// <param name="dgGrayValue">背前景灰色界限</param> /// <returns></returns> public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue) { Color piexl; string code = ""; for (int posy = 0; posy < singlepic.Height; posy++) for (int posx = 0; posx < singlepic.Width; posx++) { piexl = singlepic.GetPixel(posx, posy); if (piexl.R < dgGrayValue) // Color.Black ) code = code + "1"; else code = code + "0"; } return code; } } }