VC + + uses CImage to convert BMP images in memory jpeg

Source: Internet
Author: User
Tags bmp image

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.