如何捕捉屏幕并忽略图像中的特定窗口
问题描述:
我想编写一个程序来放大屏幕中间的位置,所以我使用了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? 但它没有很好地工作,因为PictureBox的刷新在60Hz的速度,这导致它结结巴巴。
为了清楚起见,我的想法是放大屏幕中央,然后在屏幕中间的正方形中显示放大的图像(正方形外部的区域将保留其原始尺寸)。
答
当您捕捉到初始图像时,通过在同一区域上绘制一个矩形区域,将空白区域对应于表单内部。您可以通过Form
获取窗口位置和尺寸,并在捕获图像的该部分上绘制矩形。
那个矩形会是什么? –
任何你喜欢的东西,但留下它白色/灰色/黑色通常是 – DiskJunky
我编辑我的问题,因为我认为你误解了我。如果我在屏幕中间画一个正方形,并且每帧都显示我的表单,它会停留在那里,而不需要我想要的缩放,因为我想以我想在屏幕截图中忽略的形式显示放大的图像。 –