Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load QImage from byte array without knowing the format

Tags:

c++

qt

qimage

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?

like image 520
Tomáš Zato - Reinstate Monica Avatar asked Mar 24 '26 04:03

Tomáš Zato - Reinstate Monica


1 Answers

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);
like image 66
perivesta Avatar answered Mar 26 '26 17:03

perivesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!