[英]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.