Windows中使用GDI抓图

  GDI(Graphics Device Interface)是Windows操作系统中的图形设备接口,用于提供图形绘制和显示功能。它是一组函数和数据结构的集合,允许开发人员在Windows应用程序中进行图形操作,包括绘制图形、处理图像、显示文本等。

GDI可以用于创建和管理设备上下文(Device Context,DC),设备上下文是与图形设备相关联的对象,用于进行绘图操作。通过设备上下文,可以在屏幕、打印机、图像文件等不同的设备上进行绘制。

以下是一些常用的GDI功能和使用方法:

  1. 绘制图形:使用GDI函数(如LineToRectangleEllipse等)可以在设备上下文中绘制直线、矩形、椭圆等基本图形。

  2. 显示文本:使用GDI函数(如TextOutDrawText等)可以在设备上下文中显示文本。

  3. 处理图像:GDI提供了一些函数(如LoadImageStretchBlt等)来加载、显示和处理图像。它支持多种图像格式,可以进行缩放、裁剪、旋转等操作。

  4. 颜色和画笔:GDI使用颜色和画笔对象来确定绘制图形的颜色和样式。可以使用函数(如CreatePenSelectObject等)创建和选择画笔对象,然后在设备上下文中使用画笔来绘制图形。

  5. 背景绘制:可以使用函数(如FillRectBitBlt等)来填充设备上下文的区域或复制图像到设备上下文中。

  6. 双缓冲绘制:为了减少闪烁和提高绘图性能,可以使用双缓冲技术,先将绘图操作绘制到内存中的缓冲区,然后再将整个缓冲区绘制到设备上下文中。

  7. 图形转换:GDI提供了函数(如SetViewportOrgExSetWindowOrgEx等)来进行坐标转换,可以实现平移、缩放、旋转等图形转换操作。

  8. 打印输出:GDI可以与打印机设备进行交互,使用函数(如StartDocStartPage等)可以在打印机上输出图形和文本。

如果要在Qt中使用GDI截取桌面图像,首先要在pro文件中添加gdi32

复制代码
QT       += core gui winextras

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

LIBS += -lgdi32

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    screen_capture.cpp

HEADERS += \
    screen_capture.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
复制代码

截取屏幕图像源码如下:

复制代码
#include <QDateTime>
#include <QDebug>
#include <QPixmap>
#include <QtWin>
#include <Windows.h>

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

    // 获取屏幕的设备上下文
    HDC hScreenDC = GetDC(nullptr);

    // 获取屏幕的宽度和高度
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);

    int counts = 0;
    /* 每隔1秒获取一次图像 */
    while (counts < 5) {
        QString fileName = QString("%1.png").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss"));
        qDebug() << fileName;

        // 创建一个内存设备上下文
        HDC hMemDC = CreateCompatibleDC(hScreenDC);

        // 创建一个位图对象,用于存储屏幕图像
        HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, screenWidth, screenHeight);

        // 将位图对象选入内存设备上下文
        HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);

        // 从屏幕设备上下文复制图像到内存设备上下文
        BitBlt(hMemDC, 0, 0, screenWidth, screenHeight, hScreenDC, 0, 0, SRCCOPY);

        QPixmap pixmap = QtWin::fromHBITMAP(hBitmap);
        pixmap.save(fileName, "PNG");

        // 清理资源
        SelectObject(hMemDC, hOldBitmap);
        DeleteObject(hBitmap);
        DeleteDC(hMemDC);

        counts++;
        Sleep(1000);
    }

    ReleaseDC(nullptr, hScreenDC);

    return a.exec();
}
复制代码

 

posted @   TechNomad  阅读(297)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示