在头文件中先包含GDI+的头和库
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#include
#endif
在类中定义相关成员变量
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
初始化gdi+
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
结束时销毁gdi+
GdiplusShutdown(m_gdiplusToken);
转换函数
BOOL CtojpgDlg::MBmpToMImage(CFile& cbfBmp, CFile& cbfImage, CString strType) { int iBmpSize = cbfBmp.GetLength(); HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize); if (hMemBmp == NULL) return FALSE; IStream* pStmBmp = NULL; CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp); if (pStmBmp == NULL) { GlobalFree(hMemBmp); return FALSE; } BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp); cbfBmp.SeekToBegin(); cbfBmp.Read(pbyBmp, iBmpSize); Image* imImage = NULL; imImage = Image::FromStream(pStmBmp, FALSE); if (imImage == NULL) { GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); return FALSE; } USES_CONVERSION; CLSID clImageClsid; GetImageCLSID(_T("image/jpeg"), &clImageClsid); HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0); if (hMemImage == NULL) { pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); if (imImage != NULL) delete imImage; return FALSE; } IStream* pStmImage = NULL; CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage); if (pStmImage == NULL) { pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); if (imImage != NULL) delete imImage; return FALSE; } imImage->Save(pStmImage, &clImageClsid); if (pStmImage == NULL) { pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); if (imImage != NULL) delete imImage; return FALSE; } LARGE_INTEGER liBegin = {0}; pStmImage->Seek(liBegin, STREAM_SEEK_SET, NULL); BYTE* pbyImage = (BYTE *)GlobalLock(hMemImage); cbfImage.SeekToBegin(); cbfImage.Write(pbyImage, GlobalSize(hMemImage)); if (imImage != NULL) delete imImage; pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalUnlock(hMemImage); GlobalFree(hMemBmp); GlobalFree(hMemImage); return TRUE; } BOOL CtojpgDlg:: GetImageCLSID(const WCHAR* format, CLSID* pCLSID) { UINT num = 0; UINT size = 0; ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size == 0){ return FALSE; } pImageCodecInfo = (ImageCodecInfo *)(malloc(size)); if(pImageCodecInfo == NULL) return FALSE; GetImageEncoders(num, size, pImageCodecInfo); // Find for the support of format for image in the windows for(UINT i = 0; i < num; ++i) { //MimeType: Depiction for the program image if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0) { *pCLSID = pImageCodecInfo[i].Clsid; free(pImageCodecInfo); return TRUE; } } free(pImageCodecInfo); return FALSE; }
上面的方法适用于在知道BMP位图文件时使用,而从IPicture转换到jpg位图就要稍微改下
LONG ExportShowPic( LONG nPicIndex, LPCTSTR strSavePath ) //nPicIndex为IPICTURE数组对象索引, { //strSavePath 为jpg图像保存路径 if(m_pIPicture[nPicIndex] == NULL) { return FALSE; } BOOL bResult = FALSE; //int iBmpSize = cbfBmp.GetLength(); HGLOBAL hMemBmp = GlobalAlloc(GMEM_MOVEABLE, 0); if (hMemBmp == NULL) return FALSE; IStream* pStmBmp = NULL; CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp); if (pStmBmp == NULL) { GlobalFree(hMemBmp); return FALSE; } //BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp); //cbfBmp.SeekToBegin(); //cbfBmp.Read(pbyBmp, iBmpSize); long OutStream = 0; m_pIPicture[nPicIndex]->SaveAsFile(pStmBmp, TRUE, &OutStream); // Copy Data Stream Image* imImage = NULL; imImage = Image::FromStream(pStmBmp, FALSE); if (imImage == NULL) { GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); return FALSE; } USES_CONVERSION; CLSID clImageClsid; GetImageCLSID(_T("image/jpeg"), &clImageClsid); HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0); if (hMemImage == NULL) { pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); if (imImage != NULL) delete imImage; return FALSE; } IStream* pStmImage = NULL; CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage); if (pStmImage == NULL) { pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); if (imImage != NULL) delete imImage; return FALSE; } imImage->Save(pStmImage, &clImageClsid); if (pStmImage == NULL) { pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); if (imImage != NULL) delete imImage; return FALSE; } LARGE_INTEGER liBegin = {0}; pStmImage->Seek(liBegin, STREAM_SEEK_SET, NULL); BYTE* pbyImage = (BYTE *)GlobalLock(hMemImage); //cbfImage.SeekToBegin(); //cbfImage.Write(pbyImage, GlobalSize(hMemImage)); CFile fileJpg; if(fileJpg.Open(strSavePath, CFile::modeCreate | CFile::modeWrite)) { fileJpg.SeekToBegin(); fileJpg.Write(pbyImage, GlobalSize(hMemImage)); } if (imImage != NULL) delete imImage; pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalUnlock(hMemImage); GlobalFree(hMemBmp); GlobalFree(hMemImage); bResult = TRUE; return TRUE; }