我正在用 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 代码更小,我还测试了桌面的 native Windows 句柄是否与 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 个屏幕组成的中继桌面。一个想法是使用 native Windows 调用并将图像保存在临时文件中并从那里加载 QPixmap,但是在没有 MFC 的 c++ 文件中保存 HBITMAP 并不简单。
结论:您认为 Qt 中的错误是什么?知道如何解决它(没有 MFC)
最佳答案
您可以使用 QDesktopWidget::screenCount()
计算屏幕数(Qt 4.6) 然后遍历所有屏幕并执行QPixmap::grabWindow(...)
关于“由 2 个屏幕组成的桌面”。这完全取决于 2 个屏幕是否是虚拟的(所有屏幕都视为一个屏幕)。
@Frerich Raabe:它适用于 Windows 7,因为我正在使用类似的代码来执行屏幕抓取。
关于qt - 使用 Qt4 捕获多屏幕桌面图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3966429/