C# PrintWindow 返回黑色图像

发布于 2024-11-26 04:37:11 字数 1194 浏览 0 评论 0原文

我使用的是 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;
        }
    }

I am on windows XP and I am trying to captrue a window.

But when I capture a window, I get the window title (Name & Icon) but the entire content of the window is black.

When trying to save the image, the whole image is transparent.

This is my code :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2024-12-03 04:37:11

请参阅C# 捕获直接 3D 屏幕
打印屏幕捕获可见区域,而不是手柄。

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

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

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

只需尝试启动游戏并听听它的窗口消息即可。您将看到没有 WM_PAINT 消息。所以 Windows 甚至不知道是否发生了任何变化。

Look at C# Capturing Direct 3D Screen.
Print screen captures the visible area, not a handle.

DirectX and OpenGL draw directly via hardware. With PrintScreen you can only capture handle screen which are drawn managed by Windows.

If you only need the visible area use Graphics.CaptureFromScreen.

I've tried BitBlt and PrintScreen with an OpenGL demo from http://magnum.dimajix.de/download/demos.shtml without success. PrintScreen only returned a blank bitmap and BitBlt return an old capture(probably from the first and only WM_PAINT message).

Just try to start your game and listen to it's window-messages. You will see there is no WM_PAINT message. So Windows doesn't even know if there has anything changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文