gylns gylns - 2 months ago 21
C++ Question

How to use GDI+ library to decode a jpeg in memory?

GDI+ provides a Image class, and you can use this class to read a image file with one format and then save this file to another format. But if I want to just decode a jpeg file (already loaded into memory), how can I do it?

Answer

You can use SHCreateMemStream

IStream* stream = SHCreateMemStream(buf, bufsize);
Gdiplus::Image *image = Gdiplus::Image::FromStream(stream);

Gdiplus::Graphics g(hdc);
g.DrawImage(image,0,0);

Where buf contains jpeg data (or any other compatible image format) and bufsize is the length of that data.

SHCreateMemStream needs "Shlwapi.lib" library.

Comments