bmp与png格式转换
BOOL BmpToPNG(wchar_t* strBmpFile, wchar_t* strPngFile) { FILE* cbfBmp = _wfopen(strBmpFile, L"rb"); FILE* cbfImage = _wfopen(strPngFile, L"wb"); fseek(cbfBmp, 0, SEEK_END); int iBmpSize = ftell(cbfBmp); fseek(cbfBmp, 0, SEEK_SET); HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize); if (hMemBmp == NULL) return FALSE; IStream* pStmBmp = NULL; CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp); if (pStmBmp == NULL) { GlobalFree(hMemBmp); fclose(cbfBmp); fclose(cbfImage); return FALSE; } BYTE* pbyBmp = (BYTE*)GlobalLock(hMemBmp); fread(pbyBmp, iBmpSize, 1, cbfBmp); Image* imImage = NULL; imImage = Image::FromStream(pStmBmp, FALSE); if (imImage == NULL) { GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); fclose(cbfBmp); fclose(cbfImage); return FALSE; } USES_CONVERSION; CLSID clImageClsid; GetImageCLSID(A2W("image/png"), &clImageClsid); HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0); if (hMemImage == NULL) { pStmBmp->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); if (imImage != NULL) delete imImage; fclose(cbfBmp); fclose(cbfImage); 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; fclose(cbfBmp); fclose(cbfImage); return FALSE; } imImage->Save(pStmImage, &clImageClsid); if (pStmImage == NULL) { pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalFree(hMemBmp); GlobalFree(hMemImage); if (imImage != NULL) delete imImage; fclose(cbfBmp); fclose(cbfImage); return FALSE; } LARGE_INTEGER liBegin = { 0 }; pStmImage->Seek(liBegin, STREAM_SEEK_SET, NULL); BYTE* pbyImage = (BYTE*)GlobalLock(hMemImage); fwrite(pbyImage, GlobalSize(hMemImage), 1, cbfImage); if (imImage != NULL) delete imImage; pStmBmp->Release(); pStmImage->Release(); GlobalUnlock(hMemBmp); GlobalUnlock(hMemImage); GlobalFree(hMemBmp); GlobalFree(hMemImage); fclose(cbfBmp); fclose(cbfImage); return TRUE; }