VC + + in JPEG and BMP image format should be often encountered, JPEG compared to BMP in the image size has a great advantage.
This article focuses on using the existing CImage class to convert in memory, without saving as a file, or introducing a third-party library.
The Libjpeg library also supports memory reading and conversion after 8, but it is cumbersome to use, requires manual compilation, is fully achievable with the CImage class, and the code is more concise.
Implementation method:
VC + + for JPEG, PNG image operation mainly use CImage, believe that many people have used the cimage load and save function from the file read or save JPEG or PNG format files, these two overloaded from the stream read and write.
Specifically, refer to msdn:http://msdn.microsoft.com/zh-cn/library/tf4bytf8.aspx
Using CImage to read a JPEG-formatted picture from memory, you only need to pass in a stream of a IStream interface, IStream objects are created using CreateStreamOnHGlobal.
The following is a function code for converting BMP in memory JPEG:
1 voidJpeg2bmp (Char* punzipdata,unsignedLongUlunzipdatalen, cimage*pimage)2 {3Hglobal Hglobal =GlobalAlloc (gmem_moveable, Ulunzipdatalen); 4 void* PData =GlobalLock (HGLOBAL); 5 memcpy (PData, Punzipdata, Ulunzipdatalen); 6 GlobalUnlock (HGLOBAL); 7 8istream* PStream =NULL; 9 if(CreateStreamOnHGlobal (Hglobal, TRUE, & pStream) = =S_OK)Ten { One CImage image; A if(SUCCEEDED (pimage->Load (PStream))) - { - the } -PStreamRelease (); - } - GlobalFree (HGLOBAL); +}
Punzipdata is a pointer to the JPEG picture data, Ulunzipdatalen is the length of the picture data.
Record, for the better of myself!
VC + + uses CImage to convert BMP images in memory jpeg