【发布时间】:2019-11-25 20:11:52
【问题描述】:
我使用了this 教程,但总是收到一条错误消息,提示“wicFactory 为 nullptr”。我正在使用 Windows 7,但此代码无法正常工作。我读过你应该使用CLSID_WICImagingfactory1 但它不起作用。
这是我的代码:
HRESULT hr;
bmp = nullptr;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;
//Creating a factory
IWICImagingFactory *wicFactory = NULL;
CoCreateInstance(
CLSID_WICImagingFactory1,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&wicFactory);
MessageBox(NULL, (LPCWSTR)wicFactory, NULL, MB_OK);
//Creating a decoder
hr = wicFactory->CreateDecoderFromFilename(
filename,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder);
//Read Frame from image
if (SUCCEEDED(hr)) {
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
//Creating a Converter
if (SUCCEEDED(hr)) {
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = wicFactory->CreateFormatConverter(&pConverter);
}
//Setup the converter
if (SUCCEEDED(hr)) {
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.0f,
WICBitmapPaletteTypeMedianCut
);
}
//Use the converter to create an D2D1Bitmap
//ID2D1Bitmap* bmp;
if (SUCCEEDED(hr))
{
hr = d2dg->pRT->CreateBitmapFromWicBitmap(
pConverter,
NULL,
&bmp
);
}
if (wicFactory)wicFactory->Release();
if (pDecoder)pDecoder->Release();
if (pConverter)pConverter->Release();
if (pSource)pSource->Release();
谁能确定这里的问题?除了将 _WIN32_WINNT 定义为 0x0600 或 0x0601 之外,我找不到有关该问题的更多信息,但这仍然无济于事...
【问题讨论】:
-
在创建工厂之前你会调用 CoInitializeEx 吗?
-
感谢您的帮助
-
是的,请记住,您应该在创建任何 COM 对象之前调用 CoInitializeEx。
-
您应该检查 HRESULT 代码
CreateDecoderFromFilename返回的内容。 -
然后,你应该通过谷歌搜索来检查这个 hr 的含义。
标签: c++ null direct2d nullptr wic