使用PrintWindow()返回黑色图像

时间:2018-11-22 10:40:16

标签: unity3d winapi capture

我不知道如何从Unity直接访问Meta2的摄像头,因此我尝试使用PrintWindow()捕获Meta2提供的网络摄像头的窗口。但是我可以捕获的所有屏幕都是黑色或灰色。并且在捕获窗口中,屏幕闪烁,好像它们之间没有框一样。如果有人能告诉我为什么这是问题,我将不胜感激。

private string windowName;

private void Start(){
    windowName = "Meta2 Webcam + Holographic Overlay"; // the name of process what I want to capture. and I can get the process name using spy++
}

public void Update()
{
    if(!Client_Connection(windowName).Equals("0")) PrintWindow(Client_Connection(windowName));
}

public IntPtr Client_Connection(string clientname)
{
    IntPtr iResulthwnd = new IntPtr();

    int inthwnd = FindWindow(null, clientname);
    //InjectedDLLWindowClass

    if (inthwnd == 0){
        iResulthwnd = new IntPtr(inthwnd);
    }else{
        iResulthwnd = new IntPtr(inthwnd);
    }

    return iResulthwnd;
}

public static void PrintWindow(IntPtr hwnd) {
    Rectangle rc = Rectangle.Empty;
    System.Drawing.Graphics gfxWin = System.Drawing.Graphics.FromHwnd(hwnd);
    rc = Rectangle.Round(gfxWin.VisibleClipBounds);

    Bitmap bmp = new Bitmap(
        rc.Width, rc.Height,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    System.Drawing.Graphics gfxBmp = System.Drawing.Graphics.FromImage(bmp);
    IntPtr hdcBitmap = gfxBmp.GetHdc();
    bool succeeded = PrintWindow(hwnd, hdcBitmap, 0);
    print ("succeeded : " + succeeded);
    gfxBmp.ReleaseHdc(hdcBitmap);
    if(!succeeded) {
        gfxBmp.FillRectangle(
            new SolidBrush(System.Drawing.Color.Gray),
            new Rectangle(Point.Empty, bmp.Size));
    }
    IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);
    GetWindowRgn(hwnd, hRgn);
    Region region = Region.FromHrgn(hRgn);
    if(!region.IsEmpty(gfxBmp)) {
        gfxBmp.ExcludeClip(region);
        gfxBmp.Clear(System.Drawing.Color.Transparent);
    }
    gfxBmp.Dispose();
    bmp.Save("Path" + System.DateTime.Now.ToString("yyyy년MM월dd일_hh시mm분ss초") + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}

0 个答案:

没有答案