C++ 将GDI Plus位图转换为QPixmap

C++ 将GDI Plus位图转换为QPixmap,c++,qt,gdi+,qimage,qpixmap,C++,Qt,Gdi+,Qimage,Qpixmap,我有一个GdiPlus::Bitmap对象,需要构建一个函数将其转换为QPixmap。我使用的是Qt5.8.0,性能是我关心的问题。我在网上找不到任何关于这方面的信息,甚至在QImages上搜索GDI+图像。我不需要访问像素数据,所以我所要做的就是将GDI+位图转换成可以在QWidget中显示的内容 到目前为止,我找到的最有希望的解决方案是在QtWin:中使用fromHBitMap(),但我没有理解位图句柄所需的知识/经验。我假设您手中有“位图”对象。 我还假设您的开发环境同时支持“Window

我有一个GdiPlus::Bitmap对象,需要构建一个函数将其转换为QPixmap。我使用的是Qt5.8.0,性能是我关心的问题。我在网上找不到任何关于这方面的信息,甚至在QImages上搜索GDI+图像。我不需要访问像素数据,所以我所要做的就是将GDI+位图转换成可以在QWidget中显示的内容

到目前为止,我找到的最有希望的解决方案是在QtWin:中使用fromHBitMap(),但我没有理解位图句柄所需的知识/经验。

我假设您手中有“位图”对象。 我还假设您的开发环境同时支持“Windows编程”和“Qt”

基于以上假设,您可以从“位图”中获取
HICON
,并将其传递到“
QPixmap QtWin::fromHICON(HICON图标
)”

步骤:

首先必须包括“
Gdiplus.h
”。 还包括“
QtWin

HICON-HICON;
状态st=.GetHICON(&hIcon);
QPixmap pixM=QtWin::fromHICON(hIcon);
试试这个。 以上代码未经测试。 它给出了一个想法

我不需要访问像素数据

你不知道,但Qt肯定知道。Qt不使用GDI来呈现小部件,而是使用在
QImage
上运行的光栅后端。在引擎盖下,一个
QPixmap
只是一个
QImage
的包装器,在两者之间转换很便宜,因为它不复制任何图像数据


因此,无论您选择什么路径,都会在某一点上涉及到
QImage
,即使它是以
QPixmap
的形式出现,我有一种方法可以做到这一点,我翻转了红色和蓝色字节,然后使用以下代码将其从Gdiplus::Bitmap转换为QPixmap:

QPixmap getAsQPixmap(Gdiplus::Bitmap bitmap)
{
   // One of my functions to flip blue and red bytes of the image
   _flipBlueWithRed();

   // m_pBytes is a pointer to an unsigned char that marks the start of the image
   // It was retrieved from locking the Gdiplus::Bitmap
   auto result = QPixmap::fromImage(QImage(m_pBytes, getWidth(), getHeight(), QImage::Format_RGB888));

   // Restore data back to original state
   _flipBlueWithRed();

   return result;
}
然而,这个方法速度很慢,需要60毫秒才能执行。所以我的新方法是将Gdiplus::位图保存到文件中,并使用该文件的路径从QPixmap的构造函数中读取此方法速度快得多,大约5毫秒。

QPixmap getAsQPixmap(GdiPlus::Bitmap bitmap)
{
    std::string path = "C:/path_to_img.....png";

    // One of my functions to unlock the Gdi+ bitmap
    _unlockGdiPlusBitmap();

    // Get Clsid
    CLSID pngClsid;
    getEncoderClsid(format_mime, &pngClsid);

    // Save bitmap
    // stringToWString() was a function that I wrote to convert a standard string to be a wide string
    bitmap->Save(stringToWString(path).c_str(), static_cast<const CLSID*>(&pngClsid));

    // Lock bitmap
    _lockGdiPlusBitmap();

    // Create the QPixmap
    QPixmap new_img(path);

    return new_img;
}
QPixmap getAsQPixmap(GdiPlus::Bitmap位图)
{
std::string path=“C:/path_to_img…..png”;
//我的一个解锁Gdi+位图的函数
_解锁GdiplusBitmap();
//获取Clsid
CLSID-pngClsid;
getEncoderClsid(格式为mime和pngClsid);
//保存位图
//stringToWString()是我编写的一个函数,用于将标准字符串转换为宽字符串
位图->保存(stringToWString(path).c_str(),static_cast(&pngClsid));
//锁定位图
_lockGdiPlusBitmap();
//创建QPixmap
QPixmap新img(路径);
返回新的\u img;
}
QPixmap getAsQPixmap(GdiPlus::Bitmap bitmap)
{
    std::string path = "C:/path_to_img.....png";

    // One of my functions to unlock the Gdi+ bitmap
    _unlockGdiPlusBitmap();

    // Get Clsid
    CLSID pngClsid;
    getEncoderClsid(format_mime, &pngClsid);

    // Save bitmap
    // stringToWString() was a function that I wrote to convert a standard string to be a wide string
    bitmap->Save(stringToWString(path).c_str(), static_cast<const CLSID*>(&pngClsid));

    // Lock bitmap
    _lockGdiPlusBitmap();

    // Create the QPixmap
    QPixmap new_img(path);

    return new_img;
}