[英]How can I capture the screen and ignore a specific window in the image
我想编写一个可以放大屏幕中间位置的程序,因此我使用了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();
我已经尝试过以下解决方案: 没有表单,如何捕获屏幕? 但效果不佳,因为图片框以60Hz的频率刷新,导致其结结。
明确地说,我的想法是放大屏幕的中间位置,然后在屏幕中间的正方形中显示放大的图像(正方形以外的区域将保留其原始大小)。
捕获初始图像时,通过在同一区域上绘制一个矩形来清空与表单内部相对应的区域。 您可以通过“ Form
获取窗口的位置和尺寸,并在捕获的图像的该部分上绘制矩形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.