Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Goal:
- Capture a winform window containing a directx game window (like screenshot, but specific handle).

Scenario:
- Inside a winform I insert a panel1 normally.
- I used the winapi SETPARENT to insert a directx main game window inside the penel1

Problem:
When capturing the winform window (this.handle), the image of the embedded game in the panel1 does not appear. Only the panel appears.
I can't use the ScreenShot method, because it will get another window over.

Example:
With screenshot (capturing the screen)
With WinAPI (capturing the specific window)

What I have tried:

C#
public Image CaptureWindow(IntPtr handle, int imgX = 0, int imgY = 0, int largura = 0, int altura = 0)            
        {
            // 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);
            if(largura == 0 || altura == 0)
            {
                largura = windowRect.right - windowRect.left;
                altura = 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, largura, altura);            
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over            
            //GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, 0, 0, GDI32.SRCCOPY);
            GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, imgX, imgY, GDI32.SRCCOPY);            
            // 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;
        }
Posted
Updated 22-May-18 16:02pm

1 solution

After use SetParent with Directx window, need to refresh the winform. Just: this.Refresh();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900