PrintWindow выдает черный\белый снимок с окна браузера в Windows10
Получаю список открытых окон через EnumWindows(user32.dll) и делаю их снимки через PrintWindow(user32.dll). В Windows7 все работает отлично, а вот в Windows10 окно бразуера на скриншоте получается белое (либо черное). Через BitBlt(gdi32.dll) тоже пробовал - таже ерунда.
окна получаю так:
class OpenWindowsGetter
{
[DllImport("USER32.DLL")]
static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.DLL")]
static extern IntPtr GetShellWindow();
[DllImport("User32.dll")]
static extern bool IsIconic(IntPtr hwnd);
[DllImport("USER32.DLL")]
static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc lpfn, int lParam);
[DllImport("USER32.DLL")]
static extern bool IsWindow(IntPtr hWnd);
delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
/// <summary>Returns a dictionary that contains the handle and title of all opened windows.</summary>
/// <returns>A dictionary that contains the handle and title of all opened windows.</returns>
public static IDictionary<IntPtr, string> GetOpenWindows()
{
IntPtr lShellWindow = GetShellWindow();
Dictionary<IntPtr, string> lWindows = new Dictionary<IntPtr, string>();
EnumWindows(
delegate (IntPtr hWnd, int lParam)
{
//if (hWnd == lShellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
if (IsIconic(hWnd)) return true;
if (!IsWindow(hWnd)) return true;
int lLength = GetWindowTextLength(hWnd);
if (lLength == 0) return true;
StringBuilder lBuilder = new StringBuilder(lLength);
GetWindowText(hWnd, lBuilder, lLength + 1);
lWindows[hWnd] = lBuilder.ToString();
return true;
}, 0);
return lWindows;
}
}
а картинку либо так:
public static class PrintWin
{
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hwnd, out Rect lpRect);
[DllImport("user32.dll")]
static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left, Top, Right, Bottom;
public Rectangle ToRectangle()
{
return new Rectangle(Left, Top, Right - Left, Bottom - Top);
}
}
public static Bitmap WindImage(IntPtr hwnd, out Rect rect)
{
GetWindowRect(hwnd, out rect);
Bitmap image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);
using (var graphics = Graphics.FromImage(image))
{
var hdcBitmap = graphics.GetHdc();
PrintWindow(hwnd, hdcBitmap, 0);
graphics.ReleaseHdc(hdcBitmap);
}
return image;
}
либо эдак:
public static class PrintWin
{
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
[DllImport("user32.dll")]
static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int x, int y, int cx, int cy, IntPtr hdcSrc, int x1, int y1, uint rop);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public Rectangle ToRectangle()
{
return new Rectangle(Left, Top, Right - Left, Bottom - Top);
}
}
[StructLayout(LayoutKind.Sequential)]
struct WINDOWINFO
{
public int cbSize;
public RECT rcWindow;
public RECT rcClient;
public int dwStyle;
public int dwExStyle;
public int dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public IntPtr atomWindowType;
public int wCreatorVersion;
}
const int SRCCOPY = 0xCC0020;
public static Bitmap WindImage(IntPtr hwnd, out RECT rect)
{
GetWindowRect(hwnd, out rect);
Bitmap image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);
using (var graphics = Graphics.FromImage(image))
{
var hdcBitmap = graphics.GetHdc();
//SetWindowLong(hwnd, (int)-16, (uint)0x02000000);
WINDOWINFO winf = new WINDOWINFO();
winf.cbSize = Marshal.SizeOf(winf);
GetWindowInfo(hwnd, ref winf);
BitBlt(hdcBitmap, 0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top, GetDC(hwnd), winf.rcClient.Left, winf.rcClient.Top, SRCCOPY);
graphics.ReleaseHdc(hdcBitmap);
}
return image;
}
всем заранее спасибо!
PS: с BitBlt что то не так делаю кстати, немного съехавшее изображение получается, но с окном браузера все равно не работает.