简体   繁体   English

在Qt中从QByteArray加载QPixmap?

[英]Load QPixmap from QByteArray in Qt?

I have a byte array with the contents of an image (in png/bmp or some other format). 我有一个包含图像内容的字节数组(以png / bmp或其他格式)。

How can I load it into a QPixmap? 如何将其加载到QPixmap中?

bool QPixmap::loadFromData ( const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Format here is string literal like "PNG" or something similar 这里的格式是字符串文字,如"PNG"或类似的东西

QPixmap p;
QByteArray pData;
// fill array with image
if(p.loadFromData(pData,"PNG"))
{
   // do something with pixmap
}

You should use the folowing, where your bytes are in the imageData variable in the format specified by the last parameter: 您应该使用下面的内容,其中您的字节在最后一个参数指定的格式的imageData变量中:

QPixmap pixmap = QPixmap::fromImage(
    QImage(
        (unsigned char *) imageData, 
        image_width, 
        image_height, 
        QImage::Format_RGB888
    )
);

Use this constructor: 使用此构造函数:

QImage ( const uchar * data, int width, int height, Format format )

Here is more info . 这是更多信息 After that, you can use QPixmap.convertFromImage() to create a pixmap. 之后,您可以使用QPixmap.convertFromImage()创建像素图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM