- 1 year ago
Can the Windows Imaging Component return a human-readable format name?
Comment
1 / 1
Yes, the Windows Imaging Component (WIC) can return a human-readable format name for an image file. You can use the IWICBitmapDecoder::GetContainerFormat
method to get a GUID representing the file format of an image, and then use the GetFormatName
function to convert the GUID to a human-readable format name.
Here is some sample C++ code that demonstrates how to do this:
cpp#include <windows.h>
#include <wincodec.h>
#include <string>
// Returns a human-readable format name for the specified GUID
std::wstring GetFormatName(const GUID& formatGuid)
{
wchar_t formatName[256] = { 0 };
UINT size = sizeof(formatName);
HRESULT hr = WICGetPixelFormatName(formatGuid, &size, formatName);
if (SUCCEEDED(hr))
{
return formatName;
}
else
{
return L"Unknown format";
}
}
int main()
{
// Open an image file
IWICImagingFactory* factory = nullptr;
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&factory));
if (FAILED(hr))
{
return hr;
}
IWICBitmapDecoder* decoder = nullptr;
hr = factory->CreateDecoderFromFilename(L"image.png", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &decoder);
if (FAILED(hr))
{
factory->Release();
return hr;
}
// Get the container format of the image
GUID formatGuid = {};
hr = decoder->GetContainerFormat(&formatGuid);
if (FAILED(hr))
{
decoder->Release();
factory->Release();
return hr;
}
// Convert the format GUID to a human-readable format name
std::wstring formatName = GetFormatName(formatGuid);
// Output the format name
wprintf(L"Image format: %s\n", formatName.c_str());
// Clean up
decoder->Release();
factory->Release();
return 0;
}
This code opens an image file, gets the container format using the GetContainerFormat
method, and then converts the format GUID to a human-readable format name using the GetFormatName
function. The resulting format name is then output to the console.