截图方法生成黑色图像 [英] Screenshot method generates black images

查看:200
本文介绍了截图方法生成黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有使用C#中的control.drawtobitmap后,我的第二个选择就是把桌面的截图和裁剪出所需的部分。
我打嗝显示出来,一旦我切换用户帐户,但该程序不会崩溃,一旦用户切换程序产生纯黑色的图像而已。

After failing to use the control.drawtobitmap in c#, my second option was to take screenshots of the desktop and crop out the desired sections. My hiccup shows up once i switch user accounts, although the program does not crash, once the user is switched the program generates pure black images only.

我用这个code作为参考:
<一href=\"http://stackoverflow.com/questions/2434156/webbrowser-drawtobitmap-or-other-methods/2434231#2434231\">WebBrowser.DrawToBitmap()或者其他方法?

I used this code as a reference: WebBrowser.DrawToBitmap() or other methods?

我想在逻辑上这是有道理的,因为这将有助于窗户节约资源。

I guess logically this makes sense as this would help windows save resources.

哪些选项/解决方案,我要在我的情况?

What options/ solutions do i have in my situation?

修改1
进行了修改以code来进行测试:

Edit 1 made a modification to the code for testing:

        int c = 0;
        while (true)
        {
            try
            {
                c++;
                Rectangle formBounds = this.Bounds;
                Bitmap bmp = new Bitmap(formBounds.Width, formBounds.Height);
                using (Graphics g = Graphics.FromImage(bmp))
                    g.CopyFromScreen(formBounds.Location, Point.Empty, formBounds.Size);
                bmp.Save("picture" + c.ToString() + ".jpg");
                Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

这完美的作品,而在用户帐户,但只要我切换用户时,它返回异常:句柄无效

this works perfectly while on the user account, but as soon as i switch users, it returns the exception: The handle is invalid.

任何想法?

编辑2:

在DrawToBitmap的错误是不完全随机...
如果我使用了code你提供的:

The bug in DrawToBitmap is not exactly random... if i used the code you supplied:

Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
this.DrawToBitmap(bmp, this.ClientRectangle);
bmp.Save(".\\picture.jpg");

它完美的作品,例如: http://oi61.tinypic.com/1z23ynp.jpg

然而,当下在网络浏览器控制我单击鼠标右键,DrawToBitmap将返回一个空白图像。

However, the moment i right-click on the web-browser control, DrawToBitmap will return a blank image.

例如: http://oi60.tinypic.com/9ay0yc.jpg

所以,我可以很容易地通过添加克服这个缺陷

So i can easily overcome this bug by adding

((Control)webbrowser1).Enabled = false;

这使得在Web浏览器的任何点击是不可能的,但不幸的是停用它会使我的项目没用,因为它的主要功能是模拟上的Web浏览器控制鼠标点击。
虽然这也可能是一个问题,如果窗口处于隐藏状态。

this makes any clicking impossible on the web-browser, but unfortunately to deactivate it would render my project useless as its main function is to emulate mouse clicks on a web-browser control. although this might also be a problem if the window is hidden.

目前即时寻找这个职位,其中code提供给你一个窗口句柄。

currently im looking at this post, where code is supplied to give you a window handle.

模拟点击进入一个隐藏的窗口
现在看来,这可能是一些价值......做看看。

Simulate click into a hidden window it seems it might be of some value... do have a look.

推荐答案

什么是你曾与DrawToBitmap的问题?
它工作正常,在这里,(W8.1,VS2013)即使有WebControl的,也切换用户后。 (但见末的条件编辑!)

What were the problems you had with DrawToBitmap? It works fine here, (W8.1, VS2013) even with a Webcontrol and also after switching users. (But see the edit at the end for the conditions!)

Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
this.DrawToBitmap(bmp, this.ClientRectangle);
// Clipboard.SetImage(bmp); for testing only
bmp.Dispose();

下面是code把你的窗口的屏幕截图:

Here is code to take a screenshot of your window:

Rectangle formBounds = this.Bounds;
Bitmap bmp = new Bitmap(formBounds.Width, formBounds.Height );
using (Graphics g = Graphics.FromImage(bmp))
       g.CopyFromScreen(formBounds.Location, Point.Empty, formBounds.Size);
//Clipboard.SetImage(bmp); for testing only
bmp.Dispose();

我可以切换用户喜欢我想要的,该计划一直工作。

I can switch users like I want, the program keeps working.

顺便说一句,你发布的链接是真的老了,很多事情可能会有所改善。

BTW, the link you posted is really old, many things may have improved.

编辑:

通过更新的问题,事情是更加清晰。

With the updated question things are a lot clearer.

所以,你要不断地让你的程序的截图,即使用户已经改变了,对不对?
你希望显示WebControl的,对吧?

So you want to continuously get a screenshot of your program even when the user has changed, right? and you want to display a WebControl, right?

一个用户可以有三种类型的桌面:登录/注销屏幕,屏幕保护屏幕和一个或多个正常的桌面画面(S)。不过,虽然用户注销他没有桌面屏幕都没有。

A user can have three types of desktop: the logon/logoff screen, the screensaver screen and one or more normal desktop screen(s). But while the user is logged off he has no desktop screen at all.

因此​​,如果用户没有活动桌面,既不 g.CopyFromScreen ,这将导致GDI错误,也不使用窗口句柄喜欢的截图方法不起作用在网络上的各种解决方案,包括你的链接导致的。这些都将在最好的,显示空白或黑屏。

Therefore the screenshot method will not work if the user has no active desktop, neither as g.CopyFromScreen, which will cause a GDI-error nor using a window handle like in the various solutions on the web, including the ones your link leads to. All these will, at best, show a blank or black screen.

所以DrawToBitmap方法是唯一可行的。

So the DrawToBitmap method is the only one that works.

您写的,它具有的随机的错误。这不是我所看到的。

You wrote that it has random errors. That's not what I see.

该问题出现在 predictably 当用户的交互 web浏览器任何办法。这包括滚动或具有或不具有导航点击。这些相互作​​用在 web浏览器将自己画一个空框,直到它的URL重新加载 - 不仅刷新 - 但真的 webBrowser1.Uri =新的重载乌里(uriPath)。这是可以做到,看到我的其他职位

The problems come in predictably when the user interacts with the WebBrowser in any way. This includes scrolling or clicking with or without navigation. After these interactions the WebBrowser will draw itself as an empty box until its URL is reloaded - not only refreshed - but really reloaded by webBrowser1.Uri = new Uri(uriPath). This can be done, see my other post

web浏览器还有另外一个问题时做一个 DrawToBitmap :它会失败(与该空盒)对于包括&LT任何页面;输入类型=文本元素。我不知道什么是要解决是最好的方法,更不用说为什么它摆在首位happends ..截图方法不具有特定的问题。

The WebBrowser also has another issue when doing a DrawToBitmap: It will fail (with the said empty box) for any pages that include an <input type="text" element. I'm not sure what's the best way to workaround this, let alone why it happends in the first place.. A screenshot method doesn't have that specific problem.

编辑2:

在code中的OP已经挖出了code其中,使用调用 PrintWindow ,似乎是解决我们所有的问题:它的工作原理而被注销,即使单击 web浏览器和擦伤的所有页面,包括那些文本输入字段后,可与Refeshing。 Hoorah!

The code the OP has dug up code which, using a call to PrintWindow, seems to solve all problems we had: It works while being logged off, works with Refeshing even after clicking in the WebBrowser and scrapes all pages, including those with textual input fields. Hoorah!

砍伐这里的松弛后是可以创建表格的副本的版本或只是 WebBroser (或任何其他控制)有或无国界:

After cutting down the slack here is a version that can create a copy of the Form or just the WebBroser (or any other Control) with or without borders:

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

这篇关于截图方法生成黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆