Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QPixmap不';抓取网页浏览器窗口_C++_Qt_Qimage_Qpixmap_Screen Grab - Fatal编程技术网

C++ QPixmap不';抓取网页浏览器窗口

C++ QPixmap不';抓取网页浏览器窗口,c++,qt,qimage,qpixmap,screen-grab,C++,Qt,Qimage,Qpixmap,Screen Grab,当我在web浏览器窗口中使用QPixmap::GrabWindow(WId)时,它只会返回黑屏 我正在使用以下代码: QScreen *screen = QGuiApplication::primaryScreen(); m_pixmap = screen->grabWindow(hW); m_image = m_pixmap.toImage(); m_image.save("p.png"); 当我打开“p.png”时,它只是一张黑色的图片。在其他窗口中,此功能运行良好 如何拍摄浏览器的

当我在web浏览器窗口中使用
QPixmap::GrabWindow(WId)
时,它只会返回黑屏

我正在使用以下代码:

QScreen *screen = QGuiApplication::primaryScreen();
m_pixmap = screen->grabWindow(hW);
m_image = m_pixmap.toImage();
m_image.save("p.png");
当我打开“p.png”时,它只是一张黑色的图片。在其他窗口中,此功能运行良好


如何拍摄浏览器的普通屏幕?

事实上,QScreen::grabWindow用于捕获图像。这是一个相当古老的API,由没有硬件加速(由处理器绘制)的程序使用。而chrome——这个软件并不古老,而且长期以来都是通过计算机绘制的

我已经编写了使用这种技术的软件。已发布的示例代码。它将由MSVC编译器在Qt 5.10库上编译,看起来没有区别,2015年或2017年。我的机器是64位的,也许这也很重要

内部有两个类:FrameBroadcast和FrameCapturer。FrameBroadcast从FrameCapturer请求具有一定时间间隔的屏幕截图,并通过信号
void frameCaptured(QSharedPointer frame)发送给订阅者
QSharedPointer一旦超出所有插槽处理程序的作用域,就会自动删除为屏幕内容分配的内存

#include <QApplication>
#include <QObject>
#include <QPixmap>
#include <QImage>
#include <QDialog>
#include <QLabel>

#include "framebroadcast.h"

/*static Frame* CopyFrame(const Frame *incomingFrame)
{
    Frame *frame = new Frame();
    frame->width=incomingFrame->width;
    frame->height=incomingFrame->height;
    frame->lenght=incomingFrame->lenght;
    frame->buffer=new unsigned char[frame->lenght];

    std::memcpy(frame->buffer,incomingFrame->buffer,frame->lenght);
    return frame;
}

static Frame* CopyFrame(const QSharedPointer<Frame> &incomingFrame)
{
    return CopyFrame(incomingFrame.data());
}*/


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QDialog *dialog = new QDialog();
    QLabel *label = new QLabel(dialog);

    FrameBroadcast *cast = new FrameBroadcast();
    QObject::connect(cast, &FrameBroadcast::frameCaptured, [=](const QSharedPointer<Frame> &frame) {

        int w = static_cast<int>(frame.data()->width);
        int h = static_cast<int>(frame.data()->height);

        QImage img(frame.data()->buffer,w,h,QImage::Format_RGBA8888);
        label->setPixmap(QPixmap::fromImage(img));
        label->resize(w,h);

        qDebug() << "Update";
    });
    cast->startCapture();

    dialog->show();

    return app.exec();
}

详细解析代码是没有意义的。它太大了,但要重新装备以满足您的需要并不困难。值得注意的是,使用了“”-Microsoft编译器功能:必要的库将在编译时自动启动(查看framecapturer.h)

什么是
hW
?:P@eyllanesc这是窗口的宽度
#pragma comment(lib,"dxgi.lib")
#pragma comment(lib,"D3D11.lib")
#pragma comment(lib,"Shcore.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"windowscodecs.lib")
#pragma comment (lib, "user32.lib")
#pragma comment (lib, "dxguid.lib")