VC ++ uses CImage to convert Jpeg images in the memory,

Source: Internet
Author: User

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!

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.