Hi,
I have been searching on google to find a way to get a bitmap of a hidden window. I thought I found it, but when tried it, it is only giving me a good image (not a black one), when my window is in front/active, when hidden it give me a black bitmap.
Here is my code:
public const int SRCCOPY = 13369376;
public const int WM_CLICK = 0x00F5;
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetDC")]
internal extern static IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
internal extern static IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
internal extern static IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
internal extern static IntPtr DeleteDC(IntPtr hDc);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
internal extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
internal extern static bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
internal extern static IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
internal extern static IntPtr DeleteObject(IntPtr hDc);
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
long lParam // second message parameter
);
static IntPtr hWnd = FindWindow(null, "My programs title");
public static Bitmap createBitmap()
{
Bitmap bmp = null;
IntPtr hdcFrom = GetDC(hWnd);
IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
//X and Y coordinates of window
int Width = 529;
int Height = 436;
IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, Width, Height);
if (hBitmap != IntPtr.Zero)
{
// adjust and copy
IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
BitBlt(hdcTo, 0, 0, Width, Height,
hdcFrom, 0, 0, SRCCOPY);
SelectObject(hdcTo, hLocalBitmap);
//We delete the memory device context.
DeleteDC(hdcTo);
//We release the screen device context.
ReleaseDC(hWnd, hdcFrom);
//Image is created by Image bitmap handle and assigned to Bitmap variable.
bmp = System.Drawing.Image.FromHbitmap(hBitmap);
//Delete the compatible bitmap object.
DeleteObject(hBitmap);
bmp.Save("here comes my link where it needs to be saved", ImageFormat.Bmp);
}
return bmp;
}
As stated above, it doesn't work when my window is hidden. Everything works except when my window is hidden. Does anyone have an idea how I can create a bitmap (not a black one) when my window is hidden?
Any help is greatly appreciated.