3

私はWindowsXPを使用していて、ウィンドウをキャプチャしようとしています。

しかし、ウィンドウをキャプチャすると、ウィンドウのタイトル(名前とアイコン)が表示されますが、ウィンドウのコンテンツ全体が黒くなります。

画像を保存しようとすると、画像全体が透明になります。

これは私のコードです:

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

1 に答える 1

3

C# Capturing Direct 3D Screen を見てください。Print screen は、ハンドルではなく、可視領域をキャプチャします。

DirectX と OpenGL は、ハードウェア経由で直接描画します。PrintScreen を使用すると、Windows によって管理されて描画されるハンドル スクリーンのみをキャプチャできます。

可視領域のみが必要な場合は、Graphics.CaptureFromScreen を使用します。

http://magnum.dimajix.de/download/demos.shtmlの OpenGL デモで BitBlt と PrintScreen を試しましたが、成功しませんでした。PrintScreen は空白のビットマップのみを返し、BitBlt は古いキャプチャを返します (おそらく最初で唯一の WM_PAINT メッセージから)。

ゲームを起動して、ウィンドウ メッセージを聞いてみてください。WM_PAINT メッセージがないことがわかります。そのため、Windows は何かが変更されたかどうかさえ知りません。

于 2011-07-26T11:54:09.080 に答える