【问题标题】:Window capture with embeded (setparent) directx game C#使用嵌入式(setparent)directx 游戏 C# 的窗口捕获
【发布时间】:2018-05-23 10:02:10
【问题描述】:

目标: - 捕获包含 directx 游戏窗口的 winform 窗口(如屏幕截图,但特定句柄)。

场景: - 在winform中我通常插入一个panel1。 - 我用winapi SETPARENT在penel1里面插入了一个directx主游戏窗口

问题: 捕获winform窗口(this.handle)时,panel1中嵌入游戏的图像没有出现。仅显示面板。 我不能使用 ScreenShot 方法,因为它会打开另一个窗口。

示例:

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;
    }

【问题讨论】:

    标签: c# directx screenshot


    【解决方案1】:

    在 Directx 窗口中使用 SetParent 后,需要刷新 winform。 只需:this.Refresh();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2014-04-29
      • 2019-04-20
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多