Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 图形图像 >

C#图片验证码破解

时间:2010-07-03 21:41来源:未知 作者:admin 点击:

1 public class GetImageValue
  2     {
  3         //设定图片RGB字符串
  4         string[] ArrayList = new string[]{
  5            

"0001110001111111011000111100000111000001110000011100000111000001110000011100

0001011000110111111100011100",  //0
  6            

"0011100011111000111110000001100000011000000110000001100000011000000110000001

1000000110001111111111111111",  //1
  7            

"0111110011111110100000110000001100000011000001100000110000011000001100000110

0000110000001111111111111111",  //2
  8            

"0111110011111111100000110000001100000110011110000111111000000111000000110000

0011100001111111111001111100",  //3
  9            

"0000110000011100000111000011110001101100011011001000110011001100111111111011

1111000011000000110000001100",  //4
 10            

"1111111111111111110000001100000011000000111110001111111000000111000000110000

0011100001111111111001111100",  //5
 11            

"0001111000111111011000010110000011000000110111101110111111100011110000010100

0001011000110110111100011110",  //6
 12            

"0111101000111111000000010000000000000011000001100000010000001100000010000001

1000000110000011000000110000",  //7
 13            

"0011111001111111011000110110001101110010001111100011111001100111110000011100

0001111000110111111100111110",  //8
 14            

"0001110001111011011000111100000111000001101000110111111100111101000000010000

0001010000110101110000111100",  //9
 15            

"0011100011111110100001001000001110000011100000111000001100000011100000111000

0011110001001111011000111000",  //10
 16            

"0000110001111100011111000000110000001100000011000000110000001100000011000000

1100000011000111110101111111",  //11
 17            

"1111100011011100000001000000011000000110000011000001100000110000011000001100

0000100000000101111011111110",  //12
 18            

"1011100011111110000000100000011000001100111100001111010000001110000001100000

0110000011101111110011111000",  //13
 19            

"0000011000001110000011100000111000010110001101100010011001100100111111111111

1111000001000000011000000110",  //14
 20            

"1111111011111110100000001000000010000000111100001111010000001110000001100000

0110000011101111110011111000",  //15
 21            

"0011110001111110110000101100000010000000101111001111111011000111100000111000

0011110001101111111000111100",  //16
 22            

"1110111111111111000000110000001000000110000001000000100000011000000100000011

0000001100000110000001100000",  //17
 23            

"0111110011111110110001101100011011000100011111000111110011001110100000111000

0011110001101101111001111100",  //18
 24            

"0111100011111110110001101000001110000011110001111111111101111011000000110000

0100100001101111110001111000",  //19
 25            

"0011110001111111110000111000000111000001111000110111111100110101000000000000

0011010000010111111000111100",  //20=9
 26            

"0011100011111100110001101000001110000001100000110000001110000011100000101000

0011110001101111010000101000",  //21=0
 27            

"0011100001011101011000111100000111000001111000110111111100111101000000010000

0011010000100111011000111100",  //22=9
 28            

"0000011000001010000011100001011000110110001101100100011001100110101110111111

1111000001100000011000000110",  //23=4
 29            

"0001111000101111011000010100000011000000110110101111110101100011110000011000

0001010000110101111000011110",  //24=6
 30            

"0011110001111110111000110100000111000001111000110111111100111101000000010000

0011000000110101101000111100",  //25=9
 31            

"1111100011110000000001100000010000000010000011000001100000110000011000000100

0000100000001111110011111110"   //26=2
 32         };
 33
 34         /// <summary>
 35         /// 获取图片验证码数字
 36         /// </summary>
 37         /// <returns></returns>
 38         public string GetImageValues()
 39         {
 40             string url = "http://xxxx.xxxx/image";
 41             WebRequest myWebRequest = WebRequest.Create(url);
 42             WebResponse myWebResponse = myWebRequest.GetResponse();
 43             Stream ReceiveStream = myWebResponse.GetResponseStream();
 44             Bitmap map = new Bitmap(ReceiveStream, false);
 45             UnCodebase ucode = new UnCodebase(map);
 46
 47             ucode.GrayByPixels(); //灰度处理
 48
 49             Bitmap[] pics = ucode.readMap();
 50             int[] gray = new int[4];
 51             for (int j = 0; j < 4; j++)
 52             {
 53                 gray[j] = ucode.GetSingleDgGrayValue(pics[j]);
 54             }
 55             string[] arr = new string[4];
 56             for (int i = 0; i < 4; i++)
 57             {
 58                 arr[i] = ucode.GetSingleBmpCode(pics[i], gray[i]);
 59             }
 60             string picnum = getPicnums(arr);
 61             return picnum;
 62         }
 63
 64         public string getPicnums(string[] arr)
 65         {
 66             string Code = "";
 67             for (int i = 0; i < 4; i++)
 68             {
 69                 string code = arr[i];   //得到代码串
 70
 71                 for (int arrayIndex = 0; arrayIndex < ArrayList.Length;

arrayIndex++)
 72                 {
 73                     //逐点判断特征码是否相同,允许误差!
 74                     char temp1, temp2;
 75                     int point = 0;
 76                     if (ArrayList[arrayIndex].Equals(code))
 77                     {
 78                         point = 0;
 79                         if (arrayIndex > 9)
 80                         {
 81                             if (arrayIndex == 20 || arrayIndex == 22 ||

arrayIndex == 25)
 82                             {
 83                                 Code = Code + "9";
 84                             }
 85                             else if (arrayIndex == 21)
 86                             {
 87                                 Code = Code + "0";
 88                             }
 89                             else if (arrayIndex == 23)
 90                             {
 91                                 Code = Code + "4";
 92                             }
 93                             else if (arrayIndex == 24)
 94                             {
 95                                 Code = Code + "6";
 96                             }
 97                             else if (arrayIndex == 26)
 98                             {
 99                                 Code = Code + "2";
100                             }
101                             else
102                             {
103                                 Code = Code + (arrayIndex - 10).ToString

();
104                             }
105                         }
106                         else
107                         {
108                             Code = Code + arrayIndex.ToString();
109                         }
110                         break;
111                     }
112                     else
113                     {
114                         //将字符串数组,直接转为单个字符进行对比,并记录

不相同的点
115                         for (int Comparison = 0; Comparison <

code.Length; Comparison++)
116                         {
117                             temp1 = arr[i][Comparison];
118                             temp2 = ArrayList[arrayIndex][Comparison];
119                             if (temp1 != temp2)
120                             {
121                                 point = point + 1;
122                             }
123                         }
124                     }
125
126                     //当不相同点的值小于10的时候,也就是说误差点小于10的

时候则直接等于此数字,否则将跳出循环继续对下一个特征码进行判断
127                     if (point < 10)
128                     {
129                         if (arrayIndex > 9)
130                         {
131                             if (arrayIndex == 20 || arrayIndex == 22 ||

arrayIndex == 25)
132                             {
133                                 Code = Code + "9";
134                             }
135                             else if (arrayIndex == 21)
136                             {
137                                 Code = Code + "0";
138                             }
139                             else if (arrayIndex == 23)
140                             {
141                                 Code = Code + "4";
142                             }
143                             else if (arrayIndex == 24)
144                             {
145                                 Code = Code + "6";
146                             }
147                             else if (arrayIndex == 26)
148                             {
149                                 Code = Code + "2";
150                             }
151                             else
152                             {
153                                 Code = Code + (arrayIndex - 10).ToString

();
154                             }
155                         }
156                         else
157                         {
158                             Code = Code + arrayIndex.ToString();
159                         }
160                         break;
161                     }
162                 }
163             }
164             return Code;
165         }
166
167
168 -------------------------图片处理类
169
170
171 class UnCodebase
172     {
173         public Bitmap bmpobj;
174         public UnCodebase(Bitmap pic)
175         {
176             bmpobj = new Bitmap(pic);    //转换为Format32bppRgb
177         }
178
179         /**/
180         /// <summary>
181         /// 根据RGB,计算灰度值
182         /// </summary>
183         /// <param name="posClr">Color值</param>
184         /// <returns>灰度值,整型</returns>
185         private int GetGrayNumColor(System.Drawing.Color posClr)
186         {
187             return (posClr.R * 19595 + posClr.G * 38469 + posClr.B *

7472) >> 16;
188         }
189
190         /**/
191         /// <summary>
192         /// 灰度转换,逐点方式
193         /// </summary>
194         public void GrayByPixels()
195         {
196             for (int i = 0; i < bmpobj.Height; i++)
197             {
198                 for (int j = 0; j < bmpobj.Width; j++)
199                 {
200                     int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j,

i));
201                     bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue,

tmpValue, tmpValue));
202                 }
203             }
204         }
205
206         /**/
207         /// <summary>
208         /// 去图形边框
209         /// </summary>
210         /// <param name="borderWidth"></param>
211         public void ClearPicBorder(int borderWidth)
212         {
213             for (int i = 0; i < bmpobj.Height; i++)
214             {
215                 for (int j = 0; j < bmpobj.Width; j++)
216                 {
217                     if (i < borderWidth || j < borderWidth || j >

bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
218                         bmpobj.SetPixel(j, i, Color.FromArgb(255, 255,

255));
219                 }
220             }
221         }
222
223         /**/
224         /// <summary>
225         /// 灰度转换,逐行方式
226         /// </summary>
227         public void GrayByLine()
228         {
229             Rectangle rec = new Rectangle(0, 0, bmpobj.Width,

bmpobj.Height);
230             BitmapData bmpData = bmpobj.LockBits(rec,

ImageLockMode.ReadWrite, bmpobj.PixelFormat);//

PixelFormat.Format32bppPArgb);
231             //    bmpData.PixelFormat = PixelFormat.Format24bppRgb;
232             IntPtr scan0 = bmpData.Scan0;
233             int len = bmpobj.Width * bmpobj.Height;
234             int[] pixels = new int[len];
235             Marshal.Copy(scan0, pixels, 0, len);
236
237             //对图片进行处理
238             int GrayValue = 0;
239             for (int i = 0; i < len; i++)
240             {
241                 GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
242                 pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue,

GrayValue)).ToArgb();      //Color转byte
243             }
244
245             bmpobj.UnlockBits(bmpData);
246         }
247
248         /**/
249         /// <summary>
250         /// 得到有效图形并调整为可平均分割的大小
251         /// </summary>
252         /// <param name="dgGrayValue">灰度背景分界值</param>
253         /// <param name="CharsCount">有效字符数</param>
254         /// <returns></returns>
255         public void GetPicValidByValue(int dgGrayValue, int CharsCount)
256         {
257             int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
258             int posx2 = 0; int posy2 = 0;
259             for (int i = 0; i < bmpobj.Height; i++)      //找有效区
260             {
261                 for (int j = 0; j < bmpobj.Width; j++)
262                 {
263                     int pixelValue = bmpobj.GetPixel(j, i).R;
264                     if (pixelValue < dgGrayValue)     //根据灰度值
265                     {
266                         if (posx1 > j) posx1 = j;
267                         if (posy1 > i) posy1 = i;
268
269                         if (posx2 < j) posx2 = j;
270                         if (posy2 < i) posy2 = i;
271                     };
272                 };
273             };
274             // 确保能整除
275             int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;  

//可整除的差额数
276             if (Span < CharsCount)
277             {
278                 int leftSpan = Span / 2;    //分配到左边的空列 ,如span为

单数,则右边比左边大1
279                 if (posx1 > leftSpan)
280                     posx1 = posx1 - leftSpan;
281                 if (posx2 + Span - leftSpan < bmpobj.Width)
282                     posx2 = posx2 + Span - leftSpan;
283            

(责任编辑:admin)
Tags:验证码
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价
  • 绘制几何图形:使用android.graphics类

    如何在和机上绘制2D图形呢?这是许多android游戏开发都是常提到的问题,在android SDK 当中,并没有Java Graphics2D的函数可以使用,...

  • .Net下二进制形式的文件(图片)的存储与读取

    看到标题,二进制形式的文件是很广的。这里我主要谈谈图片以二进制形式的数据读取和存储。 .Net下图片的常见存储与读取凡是有以下几种: 存储图片:以二进制的形式存...

  • C#图片验证码破解

    1 public class GetImageValue 2 { 3 // 设定图片RGB字符串 4 string []ArrayList = new stri...

  • ASP.NET画图系列之实现Curve图(曲线图)

    ASP.NET画图系列,今天要实现的是Curve图,如果各位朋友有什么不明白的可以留言或Email给我,我的Email:gfreesky@gmail.com 废...

  • ASP.NET画图系列之实现Bar图(柱状图)

    前面已经实现了Pie和Curve图,今天我们来实现Bar,就是柱状图,这个的实现和前面2种基本上差不多,还是让我们先看看效果再说. 效果图: 下面开始页面部分:...

  • ASP.NET画图系列之实现Pie图(圆饼图)

    ASP.NET自身提供的画图功能也是很强大的,在实际的项目中有时也会经常用到的,实现这样的功能方法很多,诸如水晶报表之类也可以实现,但有时我们只想实现单一的某些...

  • ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)

    学习整理了一下 (一).功能 用HttpHandler实现图片验证码 (二).代码如下 1. 处理程序文件 ValidateImageHandler.ashx代...

  • 两种获取灰度图像的方法

    在图像处理程序开发中,常会遇到将一幅彩色图像转换成灰度图像的情况,笔者在最近的一个项目中便遇到了这点。经过一翻努力最终解决,想想有必要分享一下,于是便写下此文。...

  • Asp.net网页截图的实现方法

    源代码如下: using System; using System.Drawing; using System.Drawing.Imaging; using S...

  • ASP.NET图片渐变轮换效果

    一.静态的 HTML HEAD title图片渐变轮换效果/title meta name=generator content=editplus meta na...

  • ASP.NET添加水印(从Discuz!Nt挖出来的)

    这是从 Discuz!Nt 挖出来的代码 添加水印的强大代码。功能很强大。可以添加图片水印和文字水印两种 编写也很好。添加进去的效果非常好。 供大家欣赏 现在就...

  • ASP.NET2.0图片格式转换

    第一步:打开页面 第二步:选择一副Jpg格式的图片 第三步:转换为GIF格式,明显看出图片画质降低。 后台代码: using System; using Sys...

  • 添加文字水印类代码

    usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Draw...

  • ASP.NET生成随机验证码示例

    常用的生成验证码程序,图片效果如下: 源程序如下: using System; using System.Drawing; using System.Drawi...