VC ++ uses CImage to convert Jpeg images in the memory,
Previously, I wrote an article titled converting Bmp images from Jpeg to CImage in memory using VC ++, and converting Jpeg to Bmp in memory using CImage.
Since Jpeg can be converted to Bmp, CImage also supports converting Bmp to Jpeg. In contrast to the Load function dependent on CImage in the previous article, Bmp is converted to Jpeg through the Save Function:
The use of IStream interface function overload, specific can refer to MSDN: http://msdn.microsoft.com/zh-cn/library/d06f3fhw.aspx
The following code uses CImage to convert Bmp to Jpeg in memory:
unsigned long BMP2JPEG(char* pUnZipData,unsigned long ulUnZipDataLen,char** pBuffer){unsigned long ulBufferLen = 0;HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, ulUnZipDataLen); void* pData = GlobalLock(hGlobal); memcpy(pData, pUnZipData, ulUnZipDataLen); GlobalUnlock(hGlobal); IStream* pStream = NULL; if(CreateStreamOnHGlobal(hGlobal, TRUE, & pStream) == S_OK) {CImage image; if (SUCCEEDED(image.Load(pStream))) {IStream* pOutStream = NULL; if(CreateStreamOnHGlobal(NULL, TRUE, & pOutStream) == S_OK) {image.Save(pOutStream, Gdiplus::ImageFormatJPEG);HGLOBAL hOutGlobal= NULL;GetHGlobalFromStream(pOutStream,&hOutGlobal);LPBYTE pBits= (LPBYTE)GlobalLock(hOutGlobal);ulBufferLen =(DWORD)GlobalSize(pBits);*pBuffer = new char[ulBufferLen];memcpy(*pBuffer, pBits, ulBufferLen);GlobalUnlock(hOutGlobal);pOutStream->Release();}} pStream->Release(); } GlobalFree(hGlobal); return ulBufferLen;}
Record, for better yourself!