我正在用 C++ 在 Qt4 中编写屏幕捕获应用程序。我在双屏上调整时遇到问题。我无法使用第二个屏幕获取图像。我尝试了一个 C# 应用程序,它将抓取一个图像中的所有桌面,我可以从那里提取每个屏幕桌面图像。
这是c#代码
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public class TestGrab
{
[STAThread]
static void Main(string[] args)
{
IntPtr hDC = WindowsNative.GetDC(WindowsNative.GetDesktopWindow());
IntPtr hBitmap = WindowsNative.GetCurrentObject(hDC,
WindowsNative.OBJ_BITMAP);
System.Drawing.Bitmap imageDesktop = System.Drawing.Image.FromHbitmap(
hBitmap);
imageDesktop.Save(@"c:\zzzzdesktop.png");
}
}
public class WindowsNative
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr ptr);
public const int OBJ_BITMAP = 7;
[DllImport("gdi32.dll")]
public static extern IntPtr GetCurrentObject(IntPtr hdc, uint
uiObjectType);
}
Qt代码更小,我还测试了桌面的本机窗口句柄是否与Qt Desktop0>winId()不同,但它们是相等的
QPixmap CaptureWinDesktop()
{
WId desktop=GetDesktopWindow();
WId desktop2=QApplication::desktop()->winId();
if(desktop!=desktop2)
{
qDebug("sunt fdiferite WId");
}
QPixmap entireDesktop= QPixmap::grabWindow(desktop);
return entireDesktop;
}
我不确定这是否是 Qt 中的错误或功能,使用相同的窗口句柄仅重试第一个桌面,而实际上桌面是由 2 个屏幕组成的中继。一种想法是使用本机 Windows 调用并将图像保存在临时文件中,然后从那里加载 QPixmap,但是在没有 MFC 的情况下将 HBITMAP 保存在 C++ 文件中并不简单。
结论:您认为 Qt 中的错误是什么?知道如何解决它(没有 MFC )
你可以用以下方法计算屏幕数量QDesktopWidget::screenCount()
(Qt 4.6) 然后遍历所有屏幕并执行QPixmap::grabWindow(...)
关于“由 2 个屏幕组成的桌面”。这完全取决于 2 个屏幕是否是虚拟的(所有屏幕均视为一个屏幕)。
@Frerich Raabe:它适用于 Windows 7,因为我使用类似的代码来执行屏幕抓取。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)