【发布时间】:2018-05-30 08:01:56
【问题描述】:
目的:通过从该窗口捕获图像并使用计时器将图像呈现在图片框中来镜像外部进程的窗口。
问题:当我的应用程序处于焦点位置时,一切正常!但是,如果我将焦点切换到另一个窗口(不是从我的应用程序),图片框将停止刷新图像。当我再次专注于我的应用程序时,一切都恢复正常。
限制:即使我的应用程序在后台,我也需要图片框继续更新。
捕捉图像并在图片框中显示的定时器:
private void timer_picBOX_refresh_Tick(object sender, EventArgs e)
{
pictureBox1.BackgroundImage = PrintScreen.CaptureWindow(GAME_MainHandle);
pictureBox1.Refresh();
}
捕获特定窗口的类:
public class class_ScreenCapture
{
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
public Image CaptureWindow(IntPtr handle, int imgX = 0, int imgY = 0, int largura = 0, int altura = 0)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
if (largura == 0 || altura == 0)
{
largura = windowRect.right - windowRect.left;
altura = windowRect.bottom - windowRect.top;
}
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, largura, altura);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, imgX, imgY, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}
【问题讨论】:
-
我认为您的问题来自这里
pictureBox1.BackgroundImage更改为pictureBox1.Image -
您能否详细介绍一下您桌面上的交互。你有1个屏幕吗?如果是这样,如果您“失去焦点”,这是否意味着该应用程序不再可见?因为您正在使用 GetDesktopWindow() 捕获您的桌面,所以如果您要捕获的窗口不可见,您将无法捕获它并将其显示在您的图片框中。
-
.background or .image 结果没有区别 // 没有编译错误。调试运行良好。 // 我正在捕获的游戏窗口处于“窗口”模式 // 我的应用程序仅被游戏窗口部分覆盖 // 我不是在捕获“屏幕”而是一个特定的窗口(通过主句柄和 DC 兼容) .重要提示:notepad、calc、webbrowser 等窗口不会出现此问题。仅发生在我从中捕获图像的游戏窗口(directx 游戏窗口)
-
当焦点位于游戏窗口时,就像我的应用程序在视觉上冻结一样。
-
我认为图片框实际上正在更新,但您看不到它,因为您从游戏中捕获的图像在游戏获得焦点后总是相同的。我会尝试在您的图片框中显示随机图像,看看是否是这种情况。由于 DirectX 直接渲染到屏幕上,因此使用 win api 可能无法捕获游戏屏幕。 Windows 可能会在游戏未获得焦点时从显卡下载图像以提供某种预览(例如在任务栏中),而当游戏获得焦点时不再需要这种预览。
标签: c# winforms visual-studio refresh picturebox