2014-09-25 82 views
0

有什么方法可以将加载的图片从网页浏览器捕获或复制到图片框?如何将网页浏览器中加载的图像复制到图片框

我想要复制的图像是“验证码”图像,并且每个请求都会发生变化。我需要在浏览器中加载的图像与图片框相同。

我试图拆分img标记并再次请求图像。它工作,但图片框图像是不一样的网络浏览器显示。

这是我迄今为止所做的。它包含一个网页浏览器,一个图片框,一个文本框和一个按钮

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) 
     { 

     } 
    } 
} 
+0

告诉我们你做了什么 – Shaharyar 2014-09-25 17:31:49

+0

好的我编辑了我的文章... – sci3nt15t 2014-09-25 17:37:00

+0

什么是调试时的'strArray2 [0]'? – 2014-09-25 17:47:38

回答

6

这我也挺棘手的一个我花了几个月来解决。第一件事是第一件事。正如你已经发现几乎所有的验证码图像都是动态生成的图像,这意味着每次你请求图像时,即使url(src标签)是相同的,总会生成一个新的验证码图像。

解决这个问题的最佳方法是从浏览器中“剪切”已经加载的验证码图像。相信我,这是最好的方式,如果不是唯一的方法。

好消息是,它可以很容易地与一个内置的方法

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; 
    } 

所以,坏消息是,C#有在drawtobitmap方法恼人的小错误(这是在MSDN网站提到) 。会发生什么情况,有时一个空白的图像会在你运行时返回......是的......当你试图破解Captchas的时候,并不是真正想要的东西!

幸运!另一个stackOverflow用户和我花了几个月的时间在这个方法的无bug版本上工作,这个版本使用了原生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; 
    } 

所以你只需拨打: CaptureWindow(webBrowser1); 这将返回整个网页浏览器的图像,然后只是剪切包含验证码图像的部分。

您可以查看我的问题是,我有类似的问题在这里(大部分甚至没有回答):

Extracting a image from a WebBrowser Control

Screenshot method generates black images

Reset Webbrowser control to update settings

现在,你有验证码您需要解密它们的图像。所以问另一个问题,给我链接和分享我的方法。 我很高兴你没有忍受我的噩梦。不要忘记将此标记为解决方案,并且非常有用!