GDI+ 是 Windows XP 後內建的圖形子系統,支援 BMP、PNG、JPEG、GIF、TIFF、EMF 等多種影像格式。本文示範如何在 VC++ 專案中初始化 GDI+,並撰寫一個通用函式 MBmpToMImage,將記憶體中的 BMP 影像轉換為指定格式,輸出至另一個 CMemFile,全程不落地檔案,適合網路傳輸或即時處理場景。
StdAfx.h 加入
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
// OnCreate 或 InitInstance
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
// OnDestroy 或 ExitInstance
GdiplusShutdown(m_gdiplusToken);
函式簽名:
BOOL MBmpToMImage(CMemFile& cbfBmp, CMemFile& cbfImage, CString strType);
cbfBmp:來源 BMP 影像記憶體檔案cbfImage:轉換後輸出的記憶體檔案strType:目標格式字串,如 "png"、"jpeg"、"gif"、"tiff"CMemFile 資料複製到 HGLOBAL,建立 IStream。Image::FromStream 載入 Image 物件。GetImageCLSID 取得目標格式的 CLSID。IStream(可動態增長),呼叫 Image::Save 存入。IStream 內容讀回 CMemFile。BOOL MBmpToMImage(CMemFile& cbfBmp, CMemFile& cbfImage, CString strType)
{
int iBmpSize = cbfBmp.GetLength();
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (!hMemBmp) return FALSE;
IStream* pStmBmp = NULL;
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
if (!pStmBmp) { GlobalFree(hMemBmp); return FALSE; }
BYTE* pbyBmp = (BYTE*)GlobalLock(hMemBmp);
cbfBmp.SeekToBegin();
cbfBmp.Read(pbyBmp, iBmpSize);
Image* pImage = Image::FromStream(pStmBmp, FALSE);
if (!pImage) {
GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); pStmBmp->Release();
return FALSE;
}
USES_CONVERSION;
CLSID clImageClsid;
CString mime = _T("image/") + strType;
GetImageCLSID(A2W(mime), &clImageClsid);
HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0);
if (!hMemImage) {
pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); delete pImage;
return FALSE;
}
IStream* pStmImage = NULL;
CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage);
if (!pStmImage) {
pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp);
GlobalFree(hMemImage); delete pImage; return FALSE;
}
Status st = pImage->Save(pStmImage, &clImageClsid);
if (st != Ok) {
pStmBmp->Release(); pStmImage->Release();
GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); delete pImage;
return FALSE;
}
LARGE_INTEGER liZero = {0};
pStmImage->Seek(liZero, STREAM_SEEK_SET, NULL);
BYTE* pbyImage = (BYTE*)GlobalLock(hMemImage);
SIZE_T imgSize = GlobalSize(hMemImage);
cbfImage.SeekToBegin();
cbfImage.Write(pbyImage, imgSize);
// 清理
delete pImage;
pStmBmp->Release(); pStmImage->Release();
GlobalUnlock(hMemBmp); GlobalUnlock(hMemImage);
GlobalFree(hMemBmp); GlobalFree(hMemImage);
return TRUE;
}
HRESULT GetImageCLSID(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0, size = 0;
GetImageEncodersSize(&num, &size);
if (size == 0) return E_FAIL;
ImageCodecInfo* pInfo = (ImageCodecInfo*)malloc(size);
GetImageEncoders(num, size, pInfo);
for (UINT i = 0; i < num; ++i) {
if (wcscmp(pInfo[i].MimeType, format) == 0) {
*pClsid = pInfo[i].Clsid;
free(pInfo); return S_OK;
}
}
free(pInfo); return E_FAIL;
}
CMemFile bmpMem, outMem;
// 假設 bmpMem 已載入 BMP 資料
if (MBmpToMImage(bmpMem, outMem, _T("png"))) {
// outMem 現在保存 PNG 資料,可寫入檔案或傳送
CFile file(_T("output.png"), CFile::modeCreate | CFile::modeWrite);
outMem.SeekToBegin();
BYTE buf[4096]; UINT n;
while ((n = outMem.Read(buf, sizeof(buf))) > 0) file.Write(buf, n);
}
GetImageCLSID 需包含 GdiPlus.h 並連結 gdiplus.lib。EncoderParameters 設定品質。CMemFile 適用於小型影像;大型影像建議使用暫存檔或串流處理。