首页 新闻 会员 周边 捐助

C#调用WinAPI截取指定句柄窗口图片图片是黑的

0
悬赏园豆:50 [已解决问题] 解决于 2014-07-19 17:33

电脑桌面上面有3个这样的窗口,通过win32截图,其中一个身份验证窗口句柄是黑的,其他窗口都截图正常,




public Image CaptureWindow(IntPtr handle,int dwRop)

{
// 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, dwRop);
// 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);
return img;
}

 

/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{

public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}

/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}

福祿娃的主页 福祿娃 | 初学一级 | 园豆:76
提问于:2014-07-19 15:59
< >
分享
最佳答案
-4

已找到问题原因,谢谢

福祿娃 | 初学一级 |园豆:76 | 2014-07-19 17:04

我也碰到这个问题了  能不能告诉我是什么原因啊 其他的窗口根据句柄截图都是好的  只有QQ的截图是黑的 没有图

吃饭带上碗 | 园豆:200 (初学一级) | 2014-12-26 11:16

@吃饭带上碗: 这是腾讯做的保密工作

魔术师LYX | 园豆:199 (初学一级) | 2015-11-09 20:41
其他回答(1)
0

你好,怎么解决的,能告知一下么?

eddyc | 园豆:202 (菜鸟二级) | 2015-12-04 16:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册