Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load QPixmap from QByteArray in Qt?

I have a byte array with the contents of an image (in png/bmp or some other format).

How can I load it into a QPixmap?

like image 931
Bokambo Avatar asked Jul 26 '11 07:07

Bokambo


2 Answers

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
}
like image 171
Raiv Avatar answered Sep 24 '22 22:09

Raiv


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
    )
);
like image 24
Matyas Avatar answered Sep 22 '22 22:09

Matyas