VC++使用CImage在内存中Bmp转换Jpeg图片

之前写了一篇《VC++使用CImage在内存中Jpeg转换Bmp图片》,通过CImage实现了在内存中Jpeg转Bmp。

既然Jpeg能转Bmp,那CImage也支持Bmp转Jpeg,与上一篇文章依赖CImage的Load函数相反,Bmp转Jpeg通过Save函数实现:

使用的也是IStream接口的函数重载,具体可以参考MSDN:http://msdn.microsoft.com/zh-cn/library/d06f3fhw.aspx

下面贴出使用CImage在内存中Bmp转换Jpeg的代码:

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;
}

记录,为更好的自己!

posted @ 2014-12-19 21:02  全栈工程狮  阅读(3364)  评论(0编辑  收藏  举报