【问题标题】:C# PrintWindow returns a black imageC# PrintWindow 返回一个黑色图像
【发布时间】:2011-10-13 05:07:24
【问题描述】:

我在 Windows XP 上,我正在尝试捕获一个窗口。

但是当我捕获一个窗口时,我得到了窗口标题(名称和图标),但窗口的整个内容都是黑色的。

当试图保存图像时,整个图像是透明的。

这是我的代码:

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);

    public void CaptureWindow(IntPtr handle)
    {
        Rectangle rect = new Rectangle();
        GetWindowRect(handle, ref rect);
        rect.Width = rect.Width - rect.X;
        rect.Height = rect.Height - rect.Y;
        try
        {
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            Graphics memoryGraphics = Graphics.FromImage(bmp);
            IntPtr hdc = memoryGraphics.GetHdc();
            PrintWindow(handle, hdc, 0);
            ResultsPB.Image = bmp;
            memoryGraphics.ReleaseHdc(hdc);
        }
        catch (Exception)
        {
            throw;
        }
    }

【问题讨论】:

  • 很好,但我需要它来玩游戏,我记得 PrintWindow 适用于游戏(在 XP 中)。我想我只需要 PrintWindow 的正确代码。我不能使用你的方法,因为 GetWindowDC() 在我正在尝试的游戏中返回 null。
  • 游戏可能使用 DirectX 或 OpenGL。要捕获这些屏幕,您必须使用 DirectX/OpenGL 捕获屏幕。
  • 好吧,我该怎么做? (而且我不确定这是要走的路,因为我什至可以使用 printscreen 捕获它)

标签: c# screen-capture graphic gdi


【解决方案1】:

看看C# Capturing Direct 3D Screen。 打印屏幕捕获可见区域,而不是句柄。

DirectX 和 OpenGL 直接通过硬件进行绘制。使用 PrintScreen 只能捕获由 Windows 管理的绘制的句柄屏幕。

如果您只需要可见区域,请使用 Graphics.CaptureFromScreen。

我已经使用来自http://magnum.dimajix.de/download/demos.shtml 的 OpenGL 演示尝试了 BitBlt 和 PrintScreen,但没有成功。 PrintScreen 仅返回一个空白位图,而 BitBlt 返回一个旧捕获(可能来自第一个也是唯一一个 WM_PAINT 消息)。

试着开始你的游戏并听它的窗口消息。您将看到没有 WM_PAINT 消息。所以 Windows 甚至不知道是否有任何变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2015-09-12
    • 2019-06-21
    相关资源
    最近更新 更多