3

编辑:最初的问题是一个很长的问题,有很多疯狂的猜测。我已经把它切回了剩下的谜团。

我现在整天都在挣扎和困惑,我想我应该向社区提出我的问题。

它起源于帖子中称为Screenshot 的方法生成黑色图像。原始发帖人希望每隔 n 秒连续截取他的程序的屏幕截图,其中包括一个 WebBrowser,即使用户已注销

当用户注销时,他不再有屏幕。因此,任何读取屏幕的尝试都将失败。如果使用窗口句柄,结果是一个黑框,使用 CopyFromScreen 时会出现 GDI 错误异常。

但是程序窗口仍然存在,DrawToBitmap即使用户注销也可以正常使用。

以下是条件和剩余问题:

  • 用户不得WebBrowser以任何方式触摸/单击。如果他这样做,比如说,滚动、单击、导航子DrawToBitmap调用会导致一个空框。

  • 虽然WebBrowser保持不变,但Refresh在下一次 DrawToBitmap 调用之前执行一次就足够了。

  • 触摸它后,需要通过执行重新加载 URL webBrowser1.Url = new Uri(URLpath);

  • 导航时必须存储新的 URL 才能执行此操作。我在导航事件中这样做。

  • 无论如何,DrawToBitmap如果网页包含<input type="text" ..> field.

  • DocumentText通过用 a破坏Replace("<input", "<in_put");可以治愈,但如果没有进一步的技巧,这将丢失 CSS 表..

测试它抛出两个Buttons, a Label, a Timer, a Combobox and a WebBrowser on a Form并复制代码;将文件路径更改为适合您的设置和观看的文件夹..:

public Form1()
{
    InitializeComponent();
    this.button1.Click += new System.EventHandler(this.button1_Click);
    this.button2.Click += new System.EventHandler(this.button2_Click);
    this.button1.Text = "Start";
    this.button2.Text = "Stop";
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    this.comboBox1.Items.AddRange(new object[] {
        "https://stackoverflow.com/questions",
        "http://webcam.zirndorf.de/marktplatz/gross.jpg"});
    scapeRect = this.ClientRectangle;
    webBrowser1.Url = new Uri("https://stackoverflow.com/questions");
    this.comboBox1.SelectedIndexChanged += 
                   new System.EventHandler(this.comboBox1_SelectedIndexChanged);

}

Rectangle scapeRect = Rectangle.Empty;
int imgIndex = 0;
int urlIndex = 0;

private void button1_Click(object sender, EventArgs e)
{
    timer1.Interval = 10 * 1000;  // every 10 seconds
    timer1.Start();
}

private void button2_Click(object sender, EventArgs e)
{
    timer1.Stop();
}


private void timer1_Tick(object sender, EventArgs e)
{
    imgIndex ++;
    label1.Text = imgIndex .ToString();
    webBrowser1.Url = new Uri(comboBox1.Text); // this works almost always
    //webBrowser1.Refresh();                   // this works only if the WB is 'untouched'   
    string filename = "d:\\scrape\\AB_sos_Screen" + imgIndex .ToString("000") + ".png";
    Bitmap bmp = new Bitmap(scapeRect.Width, scapeRect.Height);
    this.DrawToBitmap(bmp, scapeRect);
    bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
    bmp.Dispose();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.Text != "") webBrowser1.Url = new Uri(comboBox1.Text);
}




private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    if (!comboBox1.Items.Contains(e.Url.ToString()))
        urlIndex = comboBox1.Items.Add(e.Url.ToString());
    else
        urlIndex = comboBox1.Items.IndexOf(e.Url.ToString());
    if (urlIndex >= 0) comboBox1.SelectedIndex = urlIndex;
    button1.Focus();
}

我现在几乎可以自由地导航,并且屏幕抓取继续工作——除了带有文本输入字段的页面,例如用户标签页面。

我想知道是否anybdoy可以复制..?

或者解释一下??

还是我毕竟只是在“猎鬼”而事情根本不可靠???

最终编辑:

虽然得到解释会很好,但获得一个可行的解决方案可能必须足够好。OP 找到了使用PrintWindow调用user32.dll并解决所有问题的代码。它在注销时工作,Refreshing即使在点击WebBrowser并刮掉所有页面后也能工作,包括那些带有文本输入字段的页面。这是我的版本:

using System.Runtime.InteropServices;
//...
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);

public Bitmap CaptureControl(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;
}

这可以捕获带或不带边框的表单或控件。

4

1 回答 1

0

只是想补充我的经验,经过一整天的努力解决这个问题。

上述PrintWindow基于方法的方法只是在大部分WebBrowser控件上绘制了一个黑色矩形,但奇怪的是,显示文本的最后几行似乎通过了。所以即使是黑色矩形也不一致!但我能够开始DrawToBitmap()工作。

但是,有各种各样的隐藏要求。

  • 首先,您的表单中只能有一个WebBrowser控件——当我尝试添加第二个控件时,它会显示得很好,但是当绘制到位图时它会显示为空白。
  • 其次,WebBrowser必须是表单中最顶层的控件,并且不能应用任何顶部/底部边距。违反这一点往往会导致我显示的 HTML 的底部被截断,而足够大的顶部/底部边距往往会导致页面的内容在绘制为位图时被垂直拉伸。
  • 第三,为了防止WebBrowser被触碰,创建一个 disabledControl来包装它,并把它放在WebBrowser那个控件的内部(用 a Dockof Fill)。您必须处理显示整个 HTML 文档的内容,其中大部分内容都在此处Document.Body.ScrollRectangle介绍(即设置您的网络浏览器,并 在DocumentCompleted事件处理程序中将控件的大小设置为网络浏览器的大小)。

但到目前为止,这种方法对我来说一直有效。

于 2017-05-25T21:35:36.573 回答