// 获取指定图片格式CLSID
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num= 0;
UINT size= 0;
ImageCodecInfo* pImageCodecInfo= NULL;
GetImageEncodersSize(&num, &size);
if(size== 0)
{
return -1;
}
pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo== NULL)
{
return -1;
}
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j=0; j< num; ++j)
{
if(wcscmp(pImageCodecInfo[j].MimeType, format)== 0)
{
*pClsid= pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
pImageCodecInfo= NULL;
return j;
}
}
free(pImageCodecInfo);
pImageCodecInfo= NULL;
return -1;
}
// 在内存中绘制
Bitmap* pBitmap = new Bitmap(200, 400);
if (!pBitmap)
{
return;
}
Graphics* imageGraphics = Graphics::FromImage(pBitmap);
// do some draw on imageGraphics
imageGraphics->FillRectangle(.....);
.... and so on..
// save as image
CLSID pngClsid;
if (!pBitmap)
{
return FALSE;
}
// save as bmp image
GetEncoderClsid(L"image/bmp", &pngClsid);
m_pBitmap->Save("C:\\test.bmp", &pngClsid, NULL);
// save as png image
GetEncoderClsid(L"image/png", &pngClsid);
m_pBitmap->Save("C:\\test.png", &pngClsid, NULL);
// save as jpeg image
GetEncoderClsid(L"image/jpeg", &pngClsid);
m_pBitmap->Save("C:\\test.jpeg", &pngClsid, NULL);
评论