Note that there are some explanatory texts on larger screens.

plurals
  1. POPrintWindow bitmap differs from PrintScreen Key bitmap
    primarykey
    data
    text
    <p>When capturing a window manually with the <code>Print Screen</code>+<code>Alt</code> key combo, I get the following:</p> <p><img src="https://i.stack.imgur.com/aEhHY.png" alt="enter image description here"></p> <p>but if I try to do it programmatically using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162869%28v=vs.85%29.aspx" rel="nofollow noreferrer">Windows API</a>, I get this:</p> <p><img src="https://i.stack.imgur.com/VOOjh.png" alt="enter image description here"></p> <p><strong>Why the discrepancy? How do I get the first programmatically?</strong></p> <p>Here is my code:</p> <pre><code> [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); public Bitmap PrintWindow() { Bitmap bmp = new Bitmap(windowRect.Width, windowRect.Height, PixelFormat.Format32bppArgb); Graphics gfxBmp = Graphics.FromImage(bmp); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool success = PrintWindow(windowHandle, hdcBitmap, 0); gfxBmp.ReleaseHdc(hdcBitmap); if (!success) { Console.WriteLine("Error copying image"); Console.WriteLine(getLastError()); } gfxBmp.Dispose(); return bmp; } </code></pre> <hr> <p><strong>Update:</strong> Doing it with BitBlt does the same thing.</p> <p>Here's <a href="http://www.codeproject.com/Articles/14656/Sending-Input-Messages-to-Other-Windows-or-How-To" rel="nofollow noreferrer">code from CodeProject</a> that still returns a black-masked image:</p> <pre><code>public Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle,ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); // restore selection GDI32.SelectObject(hdcDest,hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle,hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); img.Save("SampleImage.png"); return img; } </code></pre> <p>I have tried many combinations of <code>CopyPixelOperation</code>, (somewhere around 15,000 of the 131,000) but it still doesn't work.</p> <p>Using Windows 8, AMD Radeon HD 6870.</p> <hr> <p><strong>Update 2</strong></p> <p>It seems that the window is transparent, allowing the blue color of the window to bleed through. When I change the window color to black (using the Windows personalization dialog), I get roughly something similar to the second window. The border is still missing though.</p> <p>I haven't found a solution, but it's insight into the problem.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload