0

I am having a problem with capturing the graphics of another proccess, because for some users it is just a pure black screen which I capture. Unfortunately I have no idea why this is happening for some users only. I am using the sub window directly instead of the window handle and I ensured by posting the address of the windowhandle and checking with spy++ that this window handle is actually the right one.

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);
   }
  }
kentor
  • 16,553
  • 20
  • 86
  • 144
  • Have you seen this answer? [Trouble capturing window with winAPI BitBlt for some applications only](http://stackoverflow.com/a/20612765/960067) Despite being Python, it sounds like a similar issue and the answer has lots of good details. – ZaXa Apr 14 '15 at 14:49
  • Thanks for this hint I am going to try the suggested approaches – kentor Apr 14 '15 at 14:55
  • Do me a favor and let me know if it helps. I do a ton of GDI stuff too and haven't hit this yet, but seems like I will eventually. – ZaXa Apr 14 '15 at 14:57
  • Compositing mode fixed it for now, but I can not use this because it is doing strange things like disappearing my proccess where I am capturing images from. I am still trying – kentor Apr 14 '15 at 15:00
  • You will start getting clips of overlapping windows and such in some modes too. It is tricky. – ZaXa Apr 14 '15 at 15:03
  • I need to make it work with my current solution anyhow :/. Otherwise i would need at least 2 weeks to change it to another capturing method – kentor Apr 14 '15 at 15:20
  • Keep in mind that a process can call [SetWindowDisplayAffinity](https://msdn.microsoft.com/en-us/library/windows/desktop/dd375340.aspx) and prevent another process from grabbing its screen contents altogether. – IInspectable Apr 14 '15 at 15:53
  • For everyone who is interessted, I tried this solution: https://github.com/spazzarama/Direct3DHook and it showed me the right contents. I am trying to adapt it to my project now. Thanks for your comments as well. Zaxa helped me a lot! – kentor Apr 14 '15 at 16:10

0 Answers0