Load QPixmap from QByteArray in Qt?

风格不统一 提交于 2019-12-10 12:59:05

问题


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?


回答1:


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
}



回答2:


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
    )
);



回答3:


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.



来源:https://stackoverflow.com/questions/6826683/load-qpixmap-from-qbytearray-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!