【问题标题】:How to copy the loaded image in webbrowser to picturebox如何将浏览器中加载的图像复制到图片框
【发布时间】:2014-11-20 13:59:50
【问题描述】:

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

我要复制的图像是“验证码”图像,每个请求都会更改。我需要网页浏览器中加载的图片和图片框一样。

我已尝试拆分 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)
        {

        }
    }
}

【问题讨论】:

  • 告诉我们你做了什么
  • 好的,我已经编辑了我的帖子...
  • 调试时strArray2[0]是什么?
  • strarray2 是来自网站源的分割图像位置...

标签: c# browser picturebox


【解决方案1】:

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

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

好消息是,它可以通过内置方法轻松完成

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 网站上提到过)。当你运行它时,有时会返回一个空白图像......是的......当你试图正确破解验证码时,这并不是你想要的!

幸运!另一个 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;
    }

所以您只需调用: CaptureWindow(webBrowser1); 这将返回整个网络浏览器的图像,然后剪掉包含验证码图像的部分。

如果我有类似的问题,你可以查看我的问题(大多数甚至没有回答):

Extracting a image from a WebBrowser Control

Screenshot method generates black images

Reset Webbrowser control to update settings

现在您已经有了解密所需的验证码图像。所以再问一个问题,把链接发给我,然后分享我的方法。 我很高兴你不必忍受我的噩梦。不要忘记将此标记为解决方案,并且很有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多