简体   繁体   中英

Load QImage from byte array without knowing the format

QImage has the following constructor:

QImage(const QString &fileName, const char *format = nullptr);

However, I am trying to parse JPEG/PNG images that are already loaded as byte array. QImage also has other constructors, but those take already uncompressed byte array of ARGB values.

Is there a way to create QImage from unparsed byte data instead of file?

You can construct an empty QImage and then later use QImage::loadFromData(const QByteArray&) without specifying a format. QImage will then try to read the image header to guess the file format.

QByteArray bytes; //contains image file contents
QImage img;
bool success = img.loadFromData(bytes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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