将位图保存到文件时出现问题.我使用How to save ID2D1Bitmap to PNG file作为参考,但与该页面中发布的错误不同.
我收到错误0x88990015 HRESULT,这意味着:所使用的资源是由另一个资源域中的渲染目标创建的.
这是我的代码:
void Wnd::SavePng(LPCWSTR Path,ID2D1Bitmap* pBit) {
CComPtr pRT;
CComPtr pB;
CComPtr pEncoder;
CComPtr pFrame;
CComPtr pStream;
WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
HRESULT Hr = m_pWICFactory->CreateBitmap(pBit->GetSize().width,pBit->GetSize().height,format,WICBitmapCacheOnLoad,&pB);
if (SUCCEEDED(Hr)) {
D2D1_RENDER_TARGET_PROPERTIES RTProps = RenderTargetProperties();
RTProps.pixelFormat = PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED);
Hr = m_pDirect2dFactory->CreateWicBitmapRenderTarget(pB,&RTProps,&pRT);
}
if (SUCCEEDED(Hr)) {
pRT->BeginDraw();
pRT->Clear();
pRT->DrawBitmap(pBit);
Hr = pRT->EndDraw();
}
if (SUCCEEDED(Hr)) {
Hr = m_pWICFactory->CreateStream(&pStream);
}
if (SUCCEEDED(Hr)) {
Hr = pStream->InitializeFromFilename(Path,GENERIC_WRITE);
}
if (SUCCEEDED(Hr)) {
Hr = m_pWICFactory->CreateEncoder(GUID_ContainerFormatPng,NULL,&pEncoder);
}
if (SUCCEEDED(Hr)) {
Hr = pEncoder->Initialize(pStream,WICBitmapEncoderNoCache);
}
if (SUCCEEDED(Hr)) {
Hr = pEncoder->CreateNewFrame(&pFrame,NULL);
}
if (SUCCEEDED(Hr)) {
Hr = pFrame->Initialize(NULL);
}
if (SUCCEEDED(Hr)) {
Hr = pFrame->SetSize(pBit->GetSize().width,pBit->GetSize().height);
}
if (SUCCEEDED(Hr)) {
Hr = pFrame->SetPixelFormat(&format);
}
if (SUCCEEDED(Hr)) {
Hr = pFrame->WriteSource(pB,NULL);
}
if (SUCCEEDED(Hr)) {
Hr = pFrame->Commit();
}
if (SUCCEEDED(Hr)) {
Hr = pEncoder->Commit();
}
}
我知道您不能将另一个工厂提供的资源与另一个工厂一起使用,但是必须有一种方法可以使这项工作正常进行.