sci*_*15t 0 c# browser picturebox
有没有办法将加载的图像从Web浏览器捕获或复制到图片框?
我要复制的图像是"验证码"图像,它将改变每个请求.我需要在Web浏览器中加载的图像与图片框相同.
我试图拆分img标签并再次请求图像.它工作但图片框图像与网络浏览器显示的图像不同.
这是我到目前为止所做的.它包含一个Web浏览器,一个图片框,一个文本框和一个按钮
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
namespace arman_dobare_kir_mishavad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str2 = webBrowser1.DocumentText;
string[] strArray2;
strArray2 = Regex.Split(Regex.Split(str2, "<img id=\"content1_imgCaptcha\" src=\"")[1], "\"");
textBox1.Text = strArray2[0];
this.pictureBox1.ImageLocation = "http://www.hashkiller.co.uk" + strArray2[0];
return;
}
public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个我非常棘手,花了我几个月才解决.首先要做的事情.正如您所发现的,几乎所有验证图像都是动态生成的图像,这意味着每次请求图像时,即使url(src标记)相同,也会始终生成新的验证码图像.
解决此问题的最佳方法是将已经加载的验证码图像"剪切"出您的webbrowser.相信我,这是最好的方式,如果不是唯一的方法.
好消息是,它可以通过内置方法轻松完成
webBrowser.DrawToBitmap(位图,矩形)
我的示例代码:(如何将webbrowser.DrawToBitmap用于特定元素)
private void button1_Click(object sender, EventArgs e)
{
int CaptchaWidth = getXoffset(webBrowser1.Document.GetElementById("Captch-Element-Name"));
int CaptchaHeight = getYoffset(webBrowser1.Document.GetElementById("Captch-Element-Name"));
Bitmap bitmap = new Bitmap(CaptchaWidth, CaptchaHeight);
webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, CaptchaWidth, CaptchaHeight));
//now load the image into your pictureBox (you might need to convert the bitmap to a image)
}
//Methods to get Co-ordinates Of an Element in your webbrowser
public int getXoffset(HtmlElement el)
{
int xPos = el.OffsetRectangle.Left;
HtmlElement tempEl = el.OffsetParent;
while (tempEl != null)
{
xPos += tempEl.OffsetRectangle.Left;
tempEl = tempEl.OffsetParent;
}
return xPos;
}
public int getYoffset(HtmlElement el)
{
int yPos = el.OffsetRectangle.Top;
HtmlElement tempEl = el.OffsetParent;
while (tempEl != null)
{
yPos += tempEl.OffsetRectangle.Top;
tempEl = tempEl.OffsetParent;
}
return yPos;
}
Run Code Online (Sandbox Code Playgroud)
所以,坏消息是c#在drawtobitmap方法中有一个恼人的小错误(在msdn网站上提到).发生的事情有时会在你运行它时返回一个空白图像....是的...当你试图破解Captchas时,并不是你想要的!
幸运的是!另一个stackOverflow用户和我花了几个月的时间研究这个方法的无错版本,它利用了原生的GDI +.
并且它工作得很好,所以如果drawtobitmap不能按照你期望的方式工作,这里有一个替代方案.
样品:
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
public Bitmap CaptureWindow(Control ctl)
{
//Bitmap bmp = new Bitmap(ctl.Width, ctl.Height); // includes borders
Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height); // content only
using (Graphics graphics = Graphics.FromImage(bmp))
{
IntPtr hDC = graphics.GetHdc();
try { PrintWindow(ctl.Handle, hDC, (uint)0); }
finally { graphics.ReleaseHdc(hDC); }
}
return bmp;
}
Run Code Online (Sandbox Code Playgroud)
所以你只需要调用:CaptureWindow(webBrowser1); 这将返回整个web浏览器的图像,然后只剪切包含验证码图像的部分.
您可以查看我的问题,我在这里遇到了类似的问题(大多数甚至没有回答):
现在你有了验证码图像,你需要解密它们.所以问另一个问题,给我链接,然后分享我的方法.我很高兴你不必忍受我的噩梦.不要忘记将此标记为解决方案,并且有用!