网上搜索的很多资料都不是我想要的,我想识别http://www.fjjj.gov.cn/Article/getcode.asp这个图片
不知道有人知道验证码识别时怎么做的,希望有人提供这方面的资料,谢谢!!!(网址也行)
-----------
ps:再次推一下我的小站:)bo工具网,大家别拿砖头砸我,哈哈
不知道有人知道验证码识别时怎么做的,希望有人提供这方面的资料,谢谢!!!(网址也行)
-----------
ps:再次推一下我的小站:)bo工具网,大家别拿砖头砸我,哈哈
解决方案 »
- 如何将一个数据库的部分内容定时复制到其他数据库中?
- 未将对象引用设置到对象的实例。
- 建立了一个DataSet1,想直接显示怎么办?
- 水晶报表 客户端打印机名称
- 关于LoginStatus和Logout控件问题,情况复杂,希望大虾们能帮帮忙
- lable 显示多行信息问题
- 研究过PetShop的人请进
- 非托管COM组件是什么?怎么用?
- gridview 设置AutoGenerateColumns ,如何给gridview中的内容加超链接
- 关于ASP.NET MVC4+Jquery EasyUi上传图片的问题?
- ashx文件里 Redirect 出错
- 高手来觖决 =====在IIS中浏览就出错说是CS0246: 找不到类型或命名空间名称“myLib”(是否缺少 using 指令或程序集引用?) ======在线等
msn [email protected]
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;public partial class RandomCreate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SetValidateCode();
Response.Cache.SetNoStore();
}
private void SetValidateCode()
{
Bitmap newBitmap = new Bitmap(45,18, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(newBitmap);
Random r = new Random();
g.Clear(Color.White);
for (int i = 0; i < 50; i++)
{
int x1 = r.Next(newBitmap.Width);
int x2 = r.Next(newBitmap.Width);
int y1 = r.Next(newBitmap.Height);
int y2 = r.Next(newBitmap.Height);
g.DrawLine(new Pen(Color.FromArgb(r.Next())), x1, y1, x2, y2);
}
for (int i = 0; i < 100; i++)
{
int x = r.Next(newBitmap.Width);
int y = r.Next(newBitmap.Height);
newBitmap.SetPixel(x, y, Color.FromArgb(r.Next()));
}
string value = GenerateRandom(4);
Session["RandCode"] = value;
Font font = new Font("Arial", 12, FontStyle.Bold);
Random rr = new Random();
int yy = rr.Next(1, 4);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 71, 23), Color.Red, Color.Blue, 1.2f, true);
g.DrawString(value, font, brush, 2, yy);
g.DrawRectangle(new Pen(Color.Silver), 0,0, 45, 17);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
newBitmap.Save(ms, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
}
private static char[] constant ={ '0','1','2','3','4','5','6','7','8','9'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(10)]);
}
return newRandom.ToString();
}
}
然后在另外一个页面调用:
<img src="RandomCreate.aspx" align="absmiddle" id="randomming" />
private void button2_Click(object sender, EventArgs e)
{
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; int _Width = _OldBitmap.Width / 4;
int _Height = _OldBitmap.Height; Bitmap[] _Bitmap = new Bitmap[4];
Rectangle _Rectangle = new Rectangle();
_Rectangle.X = 0;
_Rectangle.Y = 0;
_Rectangle.Width = _Width;
_Rectangle.Height = _Height;
for (int i = 0; i != _Bitmap.Length; i++)
{
_Bitmap[i] = _OldBitmap.Clone(_Rectangle, _OldBitmap.PixelFormat);
_Rectangle.X += _Width;
} string _Value1 =GetText(_Bitmap[0]);
string _Value2 =GetText(_Bitmap[1]);
string _Value3 =GetText(_Bitmap[2]);
string _Value4 =GetText(_Bitmap[3]); if (_Value1 == _Value2) MessageBox.Show("ok"); this.Text = Array.IndexOf(_TextBytes, _Value1).ToString() + Array.IndexOf(_TextBytes, _Value2).ToString();
this.Text += Array.IndexOf(_TextBytes, _Value3).ToString() + Array.IndexOf(_TextBytes, _Value4).ToString();
}
private string[] _TextBytes = new string[]
{
"01000000000101010101000101010100010101010001010101000101010100010101010001010101000101010100010101010001010101000101010100010101010001010101000101010100010101010001010101000101010101000000000101010101",
"01010001010101010101000000010101010101010101000101010101010101010001010101010101010100010101010101010101000101010101010101010001010101010101010100010101010101010101000101010101010100000000000101010101",
"01000000000101010101000101010100010101010101010101000101010101010101010001010101010101010001010101010101010001010101010101010001010101010101010001010101010101010001010101010101010100000000000001010101",
"01000000000101010101000101010100010101010101010101000101010101010101010001010101010100000001010101010101010101000101010101010101010001010101010101010100010101010001010101000101010101000000000101010101",
"01010101000101010101010101000001010101010101000100010101010101010001000101010101010001010001010101010001010100010101010100000000000001010101010101010001010101010101010100010101010101010100000001010101",
"00000000000001010101000101010101010101010001010101010101010100010101010101010101000000000001010101010101010101000101010101010101010001010101010101010100010101010001010101000101010101000000000101010101",
"01010000000101010101010001010101010101010001010101010101010100010101010101010101000100000001010101010000010101000101010100010101010001010101000101010100010101010001010101000101010101000000000101010101",
"00000000000001010101000101010100010101010101010100010101010101010101000101010101010101000101010101010101010001010101010101010001010101010101010100010101010101010100010101010101010101000101010101010101",
"01000000000101010101000101010100010101010001010101000101010100010101010001010101010000000001010101010001010101000101010100010101010001010101000101010100010101010001010101000101010101000000000101010101",
"01000000000101010101000101010100010101010001010101000101010100010101010001010101000101010000010101010100000001000101010101010101010001010101010101010100010101010101010100010101010101000000010101010101"
};
public string GetText(Bitmap p_Bitmap)
{
Bitmap _Bitmap = p_Bitmap;
int _Width = _Bitmap.Width;
int _Height = _Bitmap.Height;
BitmapData _Data = _Bitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadOnly, _Bitmap.PixelFormat); byte[] _DataByte = new byte[_Data.Stride * _Height]; Marshal.Copy(_Data.Scan0, _DataByte, 0, _DataByte.Length); MemoryStream _Mem = new MemoryStream();
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)
{
_Mem.WriteByte(1);
}
else
{
_Mem.WriteByte(0);
}
}
}
_Bitmap.UnlockBits(_Data); return BitConverter.ToString(_Mem.ToArray()).Replace("-", "");
}
我是把识别出来的验证码,post上去,那边的服务器提示验证码错误!
:(
楼上的有什么解决方案吗
-------
是不是类似这问题
有人帮忙解释一下吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.UI.HtmlControls;
using mshtml;
using System.Drawing.Imaging;
namespace ReadImageCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string url_1 = "http://www.fjjj.gov.cn/chs/jf_main.asp?t=1";
private string url_2 = "http://www.fjjj.gov.cn/article/getcode.asp";
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate(url_1);
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HTMLDocument doc = (HTMLDocument)this.webBrowser1.Document.DomDocument;
IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
IHTMLControlElement codeimg = null;
foreach (IHTMLImgElement img in doc.images)
{
if (img.src.ToLower().IndexOf(url_2.ToLower())!=-1)
codeimg = (IHTMLControlElement)img;
}
range.add(codeimg);
range.execCommand("Copy", false, null);
codeimg = null;
range = null;
doc = null;
if (Clipboard.ContainsImage())
{
this.pictureBox1.Image = Clipboard.GetImage();
MemoryStream ns = new MemoryStream();
this.pictureBox1.Image.Save(ns, ImageFormat.Bmp);
Bitmap sourcebm = (Bitmap)(Bitmap.FromStream(ns));
sourcebm=ConvertToGrayscale(sourcebm);
sourcebm = ConvertToMonochrome(sourcebm);
this.pictureBox1.Image = (Image)sourcebm;
string codestr = this.GetCodeString(sourcebm);
this.codeLabel.Text = this.GetCodeNumber(codestr);//数字验证码
//this.textBox1.Text += codestr + System.Environment.NewLine + System.Environment.NewLine;
ns = null;
sourcebm = null; }
else
{
MessageBox.Show("读取失败");
}
Clipboard.Clear();
} private Bitmap ConvertToGrayscale(Bitmap sourcebm)
{
//将彩色转换为灰度
for (int x = 0; x < sourcebm.Width; x++)
{
for (int y = 0; y < sourcebm.Height; y++)
{
Color c = sourcebm.GetPixel(x, y);
int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);//转换灰度的算法
sourcebm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}
return sourcebm;
}
byte critical_value = 238;//灰色背景色 private Bitmap ConvertToMonochrome(Bitmap sourcebm)
{
//去杂色,转换为黑白图片
for (int x = 0; x < sourcebm.Width; x++)
{
for (int y = 0; y < sourcebm.Height; y++)
{
Color c = sourcebm.GetPixel(x, y);
if (c.R == 238)
sourcebm.SetPixel(x, y, Color.FromArgb(255, 255, 255));
else
sourcebm.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
}
return sourcebm;
} private string GetCodeString(Bitmap sourcebm)
{
int[] widthStartX = new int[40];//图片宽40像素
int[] widthEndX = new int[40];
bool myColumn = true;
bool charStart = true;
int charNum = 0;
for (int x = 0; x < sourcebm.Width; x++)
{
myColumn = true;
for (int y = 0; y < sourcebm.Height; y++)
{
Color c = sourcebm.GetPixel(x, y);
if (c.R == 0 && charStart == false)//第一次出现黑点
{
widthStartX[charNum] = x;
charStart = true;
break;
}
if (c.R == 0 && charStart == true)//后续出现黑点
{
myColumn = false;
break;
}
}
if (myColumn == true && charStart == true && widthStartX[charNum] < x)//如果当列没有黑点并且前面出现过黑点还没结束
{
widthEndX[charNum] = x - 1;
charStart = false;
charNum++;
}
if (charStart == true && myColumn == false && x == (sourcebm.Width - 1))//如果开始出现黑点了,并且最后一列也有黑点
{
widthEndX[charNum] = x;
charStart = false;
charNum++;
}
}
string str = "";
int sp = 0;
for (int x = 0; x < sourcebm.Width; x++)
{
for (int y = 0; y < sourcebm.Height; y++)
{
sp++;
Color c1 = sourcebm.GetPixel(x, y);
if (c1.R == 0)
str = str + "1";
else
str = str + "0";
if (sp == 100)
{
sp = 0;
str = str + "|";//由此可分析出数字组成部分,即numstr
}
}
}
return str;
} private string GetCodeNumber(string str)
{
string[] num = str.Split('|');
return GetNum(num[0]) + GetNum(num[1]) + GetNum(num[2]) + GetNum(num[3]);
} private string GetNum(string str)
{
string ret = "0";
for (int i = 0; i < numstr.Length; i++)
{
if (numstr[i] == str)
{
ret = i.ToString();
break;
}
}
return ret;
} private string[] numstr = {
"0111111110100000000110000000011000000001100000000101111111100000000000000000000000000000000000000000",//0
"0100000001010000000111111111110000000001000000000100000000000000000000000000000000000000000000000000",//1
"0100000011100000010110000010011000010001100010000101110000010000000000000000000000000000000000000000",//2
"0100000010100000000110001000011000100001100010000101110111100000000000000000000000000000000000000000",//3
"0000011000000010100000110010000100001001111111111100000010010000000000000000000000000000000000000000",//4
"1111100010100010000110001000011000100001100010000110000111100000000000000000000000000000000000000000",//5
"0011111110010001000110001000011000100001100010000100000111100000000000000000000000000000000000000000",//6
"1100000000100000001110000011001000110000101100000011000000000000000000000000000000000000000000000000",//7
"0111011110100010000110001000011000100001100010000101110111100000000000000000000000000000000000000000",//8
"0111100000100001000110000100011000010001100010001001111111000000000000000000000000000000000000000000" //9
}; private void button1_Click(object sender, EventArgs e)
{
string url = url_1;
url = url + "&rnd=" + DateTime.Now.ToString();
this.webBrowser1.Navigate(url);
}
}
}
http://download.csdn.net/source/1301978
另外仿apple的导航条建议放顶端。
不要一直打开新窗口用self就行了。
另外,flash在opera里有问题。
有没有好的解决方法
yanzhengmashibie.com