只针对此验证码~!以下是源码 using System; using System.Collections.Generic; using System.Text; using System.Drawing;namespace DiscernCheckCode { class IbcCheck { public Bitmap bmpobj; public IbcCheck(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> /// <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(0, 0, 0)); } } } /// <summary> /// 灰度转换 /// </summary> /// <param name="dgClr"></param> public void GrayByPixels(int dgClr) { for (int i = 0; i < bmpobj.Height; i++) { for (int j = 0; j < bmpobj.Width; j++) { if (bmpobj.GetPixel(j, i).R < dgClr) { bmpobj.SetPixel(j, i, Color.FromArgb(0, 0, 0)); } else { bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255)); } } } } /// <summary> /// 得到有效图形 /// </summary> /// <param name="dgGrayValue"></param> 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-1, posy1-1, posx2 - posx1 + 3, posy2 - posy1 + 3); bmpobj = bmpobj.Clone(cloneRect, bmpobj.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; } } }
using System; using System.Collections.Generic; using System.Text;namespace DiscernCheckCode { class Distance { public static int LD(string s, string t) { int n = s.Length; //length of s int m = t.Length; //length of t int[,] d = new int[n + 1, m + 1]; // matrix int cost; // cost if (n == 0) return m; if (m == 0) return n; for (int i = 0; i <= n; d[i, 0] = i++) ; for (int j = 0; j <= m; d[0, j] = j++) ; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cost = (t.Substring(j - 1, 1) == s.Substring(i - 1, 1) ? 0 : 1); d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } return d[n, m]; } } }
窗体调用 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Net.Cache;namespace DiscernCheckCode { public partial class Form3 : Form { IbcCheck uc; Bitmap[] listbit; public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(textBoxUrl.Text); myReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); myReq.KeepAlive = true; myReq.Method = "GET"; myReq.ContentType = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon; .NET CLR 1.1.4322)"; //接收HTTP做出的响应 WebResponse myResp = myReq.GetResponse(); Stream ReceiveStream = myResp.GetResponseStream(); pictureBoxImage.Image = Image.FromStream(ReceiveStream); myResp.Close(); uc = new IbcCheck(new Bitmap(pictureBoxImage.Image)); } private void button2_Click(object sender, EventArgs e) { uc.GrayByPixels(180); pictureBoxImage.Image = uc.bmpobj;
} private void button3_Click(object sender, EventArgs e) { uc.GetPicValidByValue(180); pictureBoxImage.Image = uc.bmpobj; } private void button4_Click(object sender, EventArgs e) { uc.ClearPicBorder(2); pictureBoxImage.Image = uc.bmpobj; } private void button5_Click(object sender, EventArgs e) { listbit = uc.GetSplitPics(4, 1); for (int i = 0; i < 4; i++) { PictureBox pb = (PictureBox) this.Controls["pictureBox" + (i + 1)]; pb.Image = listbit[i]; } } private void button6_Click(object sender, EventArgs e) { DAL dal = new DAL();//此处为数据库 for (int i = 0; i < 4; i++) { string value = uc.GetSingleBmpCode(listbit[i], 128);//listbit[i] TextBox tb = (TextBox)this.Controls["textBox" + (i + 1).ToString()]; if (tb.Text.Length > 0) { dal.Add(tb.Text, value); } } } private void button7_Click(object sender, EventArgs e) { for (int i = 0; i < 4; i++) { getNum(uc.GetSingleBmpCode(listbit[i], 100), i + 1); } } private void getNum(string value2, int index) { DAL dal = new DAL(); List<CodeInfo> listCI = dal.GetInfo(); foreach (CodeInfo item in listCI) { string value = item.Value; string checkvalue = value2; double d = check(value, checkvalue); if (d > 0.99) { Label lable = (Label)this.Controls["label" + index]; lable.Text = item.Key; return; } } } private double check(string num1, string num2) { return 1 - (Convert.ToDouble(Distance.LD(num1, num2)) / Convert.ToDouble(((num1.Length + num2.Length)))); } } }
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;namespace DiscernCheckCode
{
class IbcCheck
{
public Bitmap bmpobj;
public IbcCheck(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>
/// <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(0, 0, 0));
}
}
}
/// <summary>
/// 灰度转换
/// </summary>
/// <param name="dgClr"></param>
public void GrayByPixels(int dgClr)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
if (bmpobj.GetPixel(j, i).R < dgClr)
{
bmpobj.SetPixel(j, i, Color.FromArgb(0, 0, 0));
}
else
{
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
}
}
/// <summary>
/// 得到有效图形
/// </summary>
/// <param name="dgGrayValue"></param>
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-1, posy1-1, posx2 - posx1 + 3, posy2 - posy1 + 3);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.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;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;namespace DiscernCheckCode
{
class Distance
{ public static int LD(string s, string t)
{ int n = s.Length; //length of s int m = t.Length; //length of t int[,] d = new int[n + 1, m + 1]; // matrix int cost; // cost if (n == 0) return m; if (m == 0) return n; for (int i = 0; i <= n; d[i, 0] = i++) ; for (int j = 0; j <= m; d[0, j] = j++) ; for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{ cost = (t.Substring(j - 1, 1) == s.Substring(i - 1, 1) ? 0 : 1); d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost); } } return d[n, m]; } }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Cache;namespace DiscernCheckCode
{
public partial class Form3 : Form
{
IbcCheck uc;
Bitmap[] listbit;
public Form3()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(textBoxUrl.Text);
myReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); myReq.KeepAlive = true;
myReq.Method = "GET";
myReq.ContentType = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon; .NET CLR 1.1.4322)"; //接收HTTP做出的响应
WebResponse myResp = myReq.GetResponse();
Stream ReceiveStream = myResp.GetResponseStream();
pictureBoxImage.Image = Image.FromStream(ReceiveStream); myResp.Close();
uc = new IbcCheck(new Bitmap(pictureBoxImage.Image));
} private void button2_Click(object sender, EventArgs e)
{
uc.GrayByPixels(180);
pictureBoxImage.Image = uc.bmpobj;
} private void button3_Click(object sender, EventArgs e)
{
uc.GetPicValidByValue(180);
pictureBoxImage.Image = uc.bmpobj;
} private void button4_Click(object sender, EventArgs e)
{
uc.ClearPicBorder(2);
pictureBoxImage.Image = uc.bmpobj;
} private void button5_Click(object sender, EventArgs e)
{
listbit = uc.GetSplitPics(4, 1);
for (int i = 0; i < 4; i++)
{
PictureBox pb = (PictureBox) this.Controls["pictureBox" + (i + 1)];
pb.Image = listbit[i];
}
} private void button6_Click(object sender, EventArgs e)
{
DAL dal = new DAL();//此处为数据库
for (int i = 0; i < 4; i++)
{
string value = uc.GetSingleBmpCode(listbit[i], 128);//listbit[i]
TextBox tb = (TextBox)this.Controls["textBox" + (i + 1).ToString()];
if (tb.Text.Length > 0)
{
dal.Add(tb.Text, value);
}
}
} private void button7_Click(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
getNum(uc.GetSingleBmpCode(listbit[i], 100), i + 1);
}
}
private void getNum(string value2, int index)
{
DAL dal = new DAL();
List<CodeInfo> listCI = dal.GetInfo();
foreach (CodeInfo item in listCI)
{ string value = item.Value;
string checkvalue = value2;
double d = check(value, checkvalue); if (d > 0.99)
{
Label lable = (Label)this.Controls["label" + index];
lable.Text = item.Key;
return;
}
}
}
private double check(string num1, string num2)
{
return 1 - (Convert.ToDouble(Distance.LD(num1, num2)) / Convert.ToDouble(((num1.Length + num2.Length))));
}
}
}