无重叠捕获

问题描述

| 我有一个.NET控件,可通过非托管代码输出视频。 我想捕获控件客户区域(一个视频帧)。 方法Control.DrawToBitmap不起作用,它输出控件背景-灰色。 然后,我尝试使用GDI的BitBlt:
    [DllImport(\"gdi32.dll\")]
    private static extern bool BitBlt(
    IntPtr hdcDest,// handle to destination DC
    int nXDest,// x-coord of destination upper-left corner
    int nYDest,// y-coord of destination upper-left corner
    int nWidth,// width of destination rectangle
    int nHeight,// height of destination rectangle
    IntPtr hdcSrc,// handle to source DC
    int nXSrc,// x-coordinate of source upper-left corner
    int nYSrc,// y-coordinate of source upper-left corner
    int dwRop // raster operation code
    );

    private const int SRCCOPY = 0xCC0020;

    private void btnSave_Click(object sender,EventArgs e)
    {
        Graphics graphic = captureBox.CreateGraphics();
        Bitmap memImage = new Bitmap(captureBox.Width,captureBox.Height,graphic);
        Graphics memGraphic = Graphics.FromImage(memImage);
        IntPtr dc1 = graphic.GetHdc();
        IntPtr dc2 = memGraphic.GetHdc();

        BitBlt(dc2,this.captureBox.ClientRectangle.Width,this.captureBox.ClientRectangle.Height,dc1,SRCCOPY);

        graphic.ReleaseHdc(dc1);
        memGraphic.ReleaseHdc(dc2);

        memImage.Save(\"capture.bmp\",ImageFormat.Bmp);
    }
它可以工作,但是问题在于它可以捕获所有内容,甚至可以捕获所捕获控件上的所有控件。 我想捕获控制客户区,即使它重叠。如何实现?     

解决方法

        这应该可以工作,但是已经有一段时间了,因为我已经编写了代码来使用winforms来完成此工作……如果不行,我也许可以重新创建它。 而不是
Graphics graphic = captureBox.CreateGraphics();
IntPtr dc1 = graphic.GetHdc();
[DllImport(\"gdi32.dll\")]
private static extern IntPtr GetDC(IntPtr hWnd)
[DllImport(\"gdi32.dll\")]
private static extern int ReleaseDc(IntPtr hWnd,IntPtr hDc)
// ...

IntPtr dc1 = GetDc(captureBox.Handle);
// ...
ReleaseDc(captureBox.Handle,dc1);
还要注意,如果这为您提供了灰色背景,则可能意味着该非托管代码不会呈现到您的窗口中,而是呈现在其顶部的另一个窗口。您可以找出spy ++是否属于这种情况。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...