C# Windows7 Winapi映像返回黑色clientrect

C# Windows7 Winapi映像返回黑色clientrect,c#,winapi,gdi,image-capture,C#,Winapi,Gdi,Image Capture,我在捕获另一个进程的图形时遇到了问题,因为对于某些用户来说,我捕获的只是一个纯黑色屏幕。不幸的是,我不知道为什么这只发生在一些用户身上。我直接使用子窗口而不是窗口句柄,我通过发布窗口句柄的地址并使用spy++检查此窗口句柄是否正确来确保 const string className = "BlueStacksApp"; const string windowName = "_ctl.Window"; processMainHWND = process.MainWindowHandle;

我在捕获另一个进程的图形时遇到了问题,因为对于某些用户来说,我捕获的只是一个纯黑色屏幕。不幸的是,我不知道为什么这只发生在一些用户身上。我直接使用子窗口而不是窗口句柄,我通过发布窗口句柄的地址并使用spy++检查此窗口句柄是否正确来确保

const string className = "BlueStacksApp";
  const string windowName = "_ctl.Window";

processMainHWND = process.MainWindowHandle;
    clientRectangleHandle = User32.FindWindowEx(processMainHWND, 0, className, windowName);

internal Bitmap MakeSnapshot(IntPtr AppWndHandle, RECT rect)
  {
   int width = rect.right - rect.left;
   int height = rect.bottom - rect.top;

   IntPtr hdcTo = IntPtr.Zero;
   IntPtr hdcFrom = IntPtr.Zero;
   IntPtr hBitmap = IntPtr.Zero;
   try
   {
    Bitmap clsRet = null;

    // get device context of the window...
    hdcFrom = User32.GetWindowDC(AppWndHandle);

    // create dc that we can draw to...
    hdcTo = GDI32.CreateCompatibleDC(hdcFrom);
    hBitmap = GDI32.CreateCompatibleBitmap(hdcFrom, width, height);


    //  validate
    if (hBitmap != IntPtr.Zero)
    {
     // adjust and copy

     IntPtr hLocalBitmap = GDI32.SelectObject(hdcTo, hBitmap);
     bool result = GDI32.BitBlt(hdcTo, 0, 0, width, height, hdcFrom, 0, 0, GDI32.SRCCOPY);

     GDI32.SelectObject(hdcTo, hLocalBitmap);
     //  create bitmap for window image...
     clsRet = Image.FromHbitmap(hBitmap);
    }
    return clsRet;
   }
   finally
   {
    //  release the unmanaged resources
    if (hdcFrom != IntPtr.Zero)
     User32.ReleaseDC(AppWndHandle, hdcFrom);
    if (hdcTo != IntPtr.Zero)
     GDI32.DeleteDC(hdcTo);
    if (hBitmap != IntPtr.Zero)
     GDI32.DeleteObject(hBitmap);
   }
  }

你看到这个答案了吗?尽管是Python,但它听起来像是一个类似的问题,答案有很多很好的细节。感谢您的提示,我将尝试建议的方法帮我一个忙,并让我知道它是否有帮助。我也做了很多GDI的东西,但还没有达到这一点,但似乎我最终会达到。合成模式现在修复了它,但我不能使用它,因为它正在做一些奇怪的事情,比如从我拍摄图像的过程中消失。我仍在尝试你将开始得到重叠窗口剪辑,在某些模式下也是如此。这很棘手。