简体   繁体   English

如何捕获屏幕并忽略图像中的特定窗口

[英]How can I capture the screen and ignore a specific window in the image

I want to write a program that will magnify the middle of the screen, so I used some methods from user32.dll. 我想编写一个可以放大屏幕中间位置的程序,因此我使用了user32.dll中的一些方法。 I managed to make my program capture the screen and refresh the image on the picturebox with 60FPS rate, but when I want to display in real time the bigger image, the program also captures my forms which displays the bigger image, thus leading to an infinite image inside an image loop. 我设法使程序捕获屏幕并以60FPS的速率刷新图片框上的图像,但是当我想实时显示较大的图像时,程序也捕获了显示较大图像的表单,从而导致了无限图像循环内的图像。 I want to capture the screen and ignore my form which displays the bigger image. 我想捕获屏幕,而忽略显示更大图像的表单。

I thought about something like I pass it the handle of the window I want to ignore and it will capture anything but the window i specified. 我考虑过类似的事情,例如将它传递给我要忽略的窗口的句柄,它将捕获除我指定的窗口以外的任何内容。 This is my code for now: 这是我现在的代码:

Bitmap CaptureWindow(IntPtr handle)
{
    IntPtr hdcSrc = User32.GetWindowDC(handle);
    User32.RECT windowRect = new User32.RECT();
    User32.GetWindowRect(handle, ref windowRect);
    int width = windowRect.right - windowRect.left;
    int height = windowRect.bottom - windowRect.top;
    IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
    IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
    IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

    GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
    GDI32.SelectObject(hdcDest, hOld);
    GDI32.DeleteDC(hdcDest);
    User32.ReleaseDC(handle, hdcSrc);

    Bitmap img = null;
    try
    {
        img = new Bitmap(Image.FromHbitmap(hBitmap), 1920 * 2, 1080 * 2);              
        using (Graphics g = Graphics.FromImage(img))
        {
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, 200, 1080));//img.Width - (img.Height/2), img.Height));
            // g.FillRectangle(Brushes.White, new Rectangle(img.Width - (img.Height / 2) + img.Height, 0, img.Width - img.Height / 2, img.Height));
        }
        GDI32.DeleteObject(hBitmap);            
    }
    catch (Exception e)
    { }
    return img;
}

The handle I pass it is from this method: 我通过的句柄是从这种方法:

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

I already tried this solution: How can i capture the screen without the Form? 我已经尝试过以下解决方案: 没有表单,如何捕获屏幕? but it didn't work well because the picturebox refreshes at 60Hz rate which causes it to stutter. 但效果不佳,因为图片框以60Hz的频率刷新,导致其结结。

To be clear, my idea is to magnify the middle of the screen and then display the magnified image in a square in the middle of the screen (the areas outside the square will be left with their original size). 明确地说,我的想法是放大屏幕的中间位置,然后在屏幕中间的正方形中显示放大的图像(正方形以外的区域将保留其原始大小)。

When you capture the initial image, blank out the area that corresponds to inside your form by drawing a rectangle over the same area. 捕获初始图像时,通过在同一区域上绘制一个矩形来清空与表单内部相对应的区域。 You can get the window position and dimensions via the Form and draw the rectangle over that portion of the captured image. 您可以通过“ Form获取窗口的位置和尺寸,并在捕获的图像的该部分上绘制矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM