The GDI+ Image::Save
method requires a CLSID parameter to specify the encoder to use. The documentation points to some sample code for getting the encoder associated with a particular MIME type, such as image/jpeg or image/png. However I'm balking at the thought of copying a half-page function just to support a 1-line debugging aid where I save an intermediate result out to disk.
Shouldn't there be a list of standard CLSIDs for the standard encoders? Where would I find such a list? I haven't been able to find one by searching Microsoft's include files.
There isn't one. I think they intended the codec list to be extensible and support plugins, but never got around to it. Given that they haven't made any changes to GDI+ in quite some time, they likely won't anytime soon. You could probably get away with generating your own hard coded list based on an enumeration of Gdiplus::GetImageEncoders.
That is:
image/bmp : {557cf400-1a04-11d3-9a73-0000f81ef32e}
image/jpeg : {557cf401-1a04-11d3-9a73-0000f81ef32e}
image/gif : {557cf402-1a04-11d3-9a73-0000f81ef32e}
image/tiff : {557cf405-1a04-11d3-9a73-0000f81ef32e}
image/png : {557cf406-1a04-11d3-9a73-0000f81ef32e}
Here's the function I routinely cut&paste between projects for getting at the CLSID of the encoder. You could modify it to be a table lookup.
HRESULT GetGdiplusEncoderClsid(__in LPCWSTR pwszFormat, __out GUID *pGUID)
{
HRESULT hr = E_FAIL;
UINT nEncoders = 0; // number of image encoders
UINT nSize = 0; // size of the image encoder array in bytes
CAutoVectorPtr<BYTE> spData;
Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
Gdiplus::Status status;
bool fFound = false;
// param check
if ((pwszFormat == NULL) || (pwszFormat[0] == 0) || (pGUID == NULL))
{
return E_POINTER;
}
*pGUID = GUID_NULL;
status = Gdiplus::GetImageEncodersSize(&nEncoders, &nSize);
if ((status != Gdiplus::Ok) || (nSize == 0))
{
return E_FAIL;
}
spData.Allocate(nSize);
if (spData == NULL)
{
return E_FAIL;
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(BYTE*)spData;
status = Gdiplus::GetImageEncoders(nEncoders, nSize, pImageCodecInfo);
if (status != Gdiplus::Ok)
{
return E_FAIL;
}
for(UINT j = 0; j < nEncoders; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, pwszFormat) == 0 )
{
*pGUID = pImageCodecInfo[j].Clsid;
fFound = true;
break;
}
}
hr = fFound ? S_OK : E_FAIL;
return hr;
}
You will probably want to use ImageCodecInfo
with GetImageEncodersSize()
and GetImageEncoders()
I'm not aware of any easier way.
EDIT: If you know specifically what you want and damn all the rest you can get away with doing something like this ...
CLSID pngClsid;
GetEncoderClsid("image/png", &pngClsid);
image.Save("imagename.png", &pngClsid, NULL);
If you just want to write a PNG, this appears to work:
// image/png : {557cf406-1a04-11d3-9a73-0000f81ef32e}
const CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3,{ 0x9a,0x73,0x00,0x00,0xf8,0x1e,0xf3,0x2e } };
stat = image->Save(L"test.png", &pngEncoderClsId, NULL);
Note the reformatting of the hex values.
来源:https://stackoverflow.com/questions/5345803/does-gdi-have-standard-image-encoder-clsids