【问题标题】:How can I capture the screen and ignore a specific window in the image如何捕获屏幕并忽略图像中的特定窗口
【发布时间】:2017-10-06 00:39:53
【问题描述】:

我想写一个放大屏幕中间的程序,所以我使用了user32.dll中的一些方法。我设法让我的程序捕获屏幕并以 60FPS 的速率刷新图片框上的图像,但是当我想实时显示更大的图像时,程序也会捕获显示更大图像的表单,从而导致无限图像循环内的图像。我想捕获屏幕并忽略显示更大图像的表单。

我想到了一些事情,比如我将要忽略的窗口句柄传递给它,它会捕获除我指定的窗口之外的任何内容。这是我现在的代码:

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

我传给它的句柄来自这个方法:

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

我已经尝试过这个解决方案:How can i capture the screen without the Form? 但它效果不佳,因为图片框以 60Hz 的频率刷新,导致它卡顿。

说清楚,我的想法是放大屏幕中间,然后将放大后的图像显示在屏幕中间的一个正方形中(正方形之外的区域将保留其原始大小)。

【问题讨论】:

    标签: c# screenshot


    【解决方案1】:

    当您捕获初始图像时,通过在同一区域上绘制一个矩形来将与表单内部对应的区域清空。您可以通过Form 获取窗口位置和尺寸,并在捕获的图像的该部分上绘制矩形。

    【讨论】:

    • 那个矩形会是什么?
    • 任何你喜欢的东西,但留下白色/灰色/黑色都是平常的
    • 我编辑了我的问题,因为我认为你误解了我。如果我在屏幕中间画一个正方形,每帧都显示我的表单,它会停留在那里而没有我想要的缩放,因为我想以我想在屏幕截图中忽略的形式显示放大的图像。跨度>
    • 矩形的绘制必须发生在缩放之前,而不是之后。除非我误解了?
    • 我添加了这些行:pictureBox1.Image = transp; pictureBox1.Image = sc.GetCurrentScreenImage(); 但它不起作用。 transp 是一个填充了我在此表单中透明的颜色的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多