2 Star 1 Fork 2

Petr Tripolsky / qt-dxgi-screenshot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

QPixmap doesn't grab web browser window

From here

Question:

When I use QPixmap::GrabWindow(WId) with web browser window it returns me just black screen. I'm using the following code: QScreen *screen = QGuiApplication::primaryScreen(); m_pixmap = screen->grabWindow(hW); m_image = m_pixmap.toImage(); m_image.save("p.png"); When I open "p.png" it is just black picture. With other windows this work well. How can I take a normal screen of browser?

Answer:

The fact is that QScreen :: grabWindow uses [Windows GDI] 1 to capture the image. This is a rather ancient API that is used by programs without hardware acceleration (drawn by the processor). And chrome - the software is not ancient and has long been drawn by means of [Windows DXGI] 2.

I already wrote software that uses this technology. Published sample code [here] 3. It is going to be compiled by the MSVC compiler on the Qt 5.10 library, seemingly no difference, 2015 or 2017. My machine is 64 bit, maybe this is also important.

введите сюда описание изображения

Inside there are two classes: FrameBroadcast and FrameCapturer. FrameBroadcast requests a screenshot with a certain time interval from FrameCapturer and sends the subscriber via the signal void frameCaptured (QSharedPointer <Frame> frame); QSharedPointer automatically deletes the memory allocated for the screen contents as soon as it goes out of scope for all slot handlers.

#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();
}

In main.cpp, a simple dialog box is created, which displays the result of the capture. Just in case, I attached a code that untie the contents of the screen from QSharedPointer, if it is not possible to place all the manipulations in one slot. It is immediately after the inclusions and commented out.

#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")

It makes no sense to parse the code in detail. It is too big, but it will not be difficult to retool to fit your needs. It is noteworthy that "[Auto-linking] 5" is used - the Microsoft compiler feature: the necessary libraries will pull themselves up at compile time (look in framecapturer.h)

MIT License Copyright (c) 2019 Petr Tripolsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Sample screenshot program that uses dxgi instead of gdi and can capture video games on windows 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/tripolskypetr/qt-dxgi-screenshot.git
git@gitee.com:tripolskypetr/qt-dxgi-screenshot.git
tripolskypetr
qt-dxgi-screenshot
qt-dxgi-screenshot
master

搜索帮助