Domanda

Sono su Windows XP e sto cercando di CapTrue una finestra.

Ma quando mi cattura una finestra, ottengo il titolo della finestra (nome e l'Icona), ma l'intero contenuto della finestra è nera.

Quando si cerca di salvare l'immagine, l'intera immagine è trasparente.

Questo è il mio codice:

    [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;
        }
    }
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top