Qt World Summit: Register Today!

[Solved] Reading image from memory

  • Hi,

    I have a image file (jpeg,bmp...) in QByteArray. I want to display it. For this I am using QLable. I tried loading it with QImage::loadFromData(), and QPixMap::LoadFromData(), but both of them fails! But if I use QImage::Load("filename") the file is shown correctly. What is the right method to display an image from buffer? Also I don't know the image type in buffer, is there any method to check the type of image?


  • I would assume that you are facing a formatting issue. Typically any format has some meta information about the object stored. For an image it would be size, color scheme and similar stuff. You need to refer to "the documentation":http://qt-project.org/doc/qt-4.8/qimage.html#image-information and find in which format the image is.

  • Different image formats have different header data in the beginning, you have to set your format straight so that QImage knows where is the dimensions, color depth and so on so it can locate and process image data properly.

    You don't know the image type in buffer? Well, who put it there? I am not aware if there is build in functionality for this in Qt but most image formats begin with a specific magic number, it gives the image type and thus also the information on the header and so on. Or you could just do a little struct or class to contain the buffer together with format information when the buffer is being filled with an image.

  • I think my problem description is not clear enough. I receive the image file from a network socket. So I have no clue about the image format.

    Is QImage is the class that I need to depend on displaying images?

    The only method I could find to load an image from buffer is "QImage::loadFromData()". the second argument for this is "const char * format". I assume it should be something similar to "JPG,JPEG,BMP (Documentation does not say anything about what a"format" is in this function). In my case the image construction phase itself is failing.

    Am I understanding the concepts in a wrong way?


  • What surprises me is QImage::load("filename") works fine, but a QByteArray containing the same data cannot be loaded correctly using QImage::loadFromData()

  • If you are receiving your image through a QTcpSocket, you may want to have a look on the "QImageReader":http://qt-project.org/doc/qt-4.8/qimagereader.html#QImageReader-2 I never used it, but it looks suitable for your purpose.

  • I have written a code block as shown below @
    QByteArray ba;
    //Append image data to ba
    QBuffer qbuff(&ba);
    QImageReader qimg;
    QImage img=qimg.read();

    But it is not showing the image! Am I doing something wrong?


  • Thanks koahnig, the above methods works

  • Glad to hear that your problem has been solved. Please mark the thread with [Solved] in the header line then.

  • I would be glad if there is an easy way to mark the thread as solved (using a button or anything like that)

Log in to reply