开发者

Load QPixmap from QByteArray in Qt?

开发者 https://www.devze.com 2023-03-22 18:08 出处:网络
I have a byte array with the conte开发者_如何学编程nts of an image (in png/bmp or some other format).

I have a byte array with the conte开发者_如何学编程nts of an image (in png/bmp or some other format).

How can I load it into a 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

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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号