BOOL CMyDirect2D::LoadFile(LPCTSTR filename, CComPtr<ID2D1Bitmap> &pBitmap)
{
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
//创建解码器
if (S_OK != m_pIWICFactory->CreateDecoderFromFilename(filename,
NULL,//解码器 默认使用NULL
GENERIC_READ, //对象访问权限
WICDecodeMetadataCacheOnLoad, //加载解码器时缓存元数据 WICDecodeMetadataCacheOnDemand -- 需要时加载
&pDecoder))
{
MessageBox(NULL, L"CreateDecoderFromFilename failed!", L"Error", MB_OK);
return FALSE;
}
//获得图像
pDecoder->GetFrame(0, &pSource);
//创建转换器
m_pIWICFactory->CreateFormatConverter(&pConverter);
//初始化转换器 -- 将帧转换为32bpppbgra
HRESULT hr = pConverter->Initialize(pSource, //输入图像
GUID_WICPixelFormat32bppPBGRA, //目标像素格式
WICBitmapDitherTypeErrorDiffusion, //指定抖动模式
NULL, //调色板
0.f, //alpha阈值
WICBitmapPaletteTypeCustom //调色板转换类型
);
pBitmap = nullptr;//释放旧图片资源
//创建位图
hr = m_pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,//要创建的位图的像素格式和dpi。像素格式必须与wicbitmapsource的像素格式匹配,否则方法将失败。
//为了防止不匹配,可以传递null或传递从调用d2d1::pixelformat助手函数获得的值,而无需指定任何参数值。
//如果dpix和dpiy均为0.0f,则使用默认dpi 96
&pBitmap
);
if (S_OK != hr)
{
MessageBox(NULL, L"CreateBitmapFromWicBitmap failed!", L"Error", MB_OK);
SAFE_RELEASE(pDecoder);
SAFE_RELEASE(pSource);
return FALSE;
}
SAFE_RELEASE(pDecoder);
SAFE_RELEASE(pSource);
SAFE_RELEASE(pConverter);
SAFE_RELEASE(pScaler);
return TRUE;
}
问题代码出现在 CreateBitmapFromWicBitmap 上面
hr 的错误是参数不正确,经过检查 上一步已经获得到正确的 pConverter 可是让然告诉我参数不正确,表示疑问。
而且还有一个问题 同样的代码 我加载一张小图 10000* 2048都是可以的,偏偏这个20000 * 2048的不行,求助各路大牛支援我一下。
在此深表感谢
https://blog.csdn.net/wwMTww/article/details/76638933