title: How to save a ID2D1Bitmap to a file using WIC?
body: <p>I have a problem with saving a bitmap into a file. I'm using <a href="https://stackoverflow.com/questions/11447489/how-to-save-id2d1bitmap-to-png-file">How to save ID2D1Bitmap to PNG file</a> as a reference, but I have a different error than the one posted in that.</p>
  
  <p>I get error 0x88990015 HRESULT, which means: The resource used was created by a render target in a different resource domain.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>void Wnd::SavePng(LPCWSTR Path,ID2D1Bitmap* pBit) {
      CComPtr&lt;ID2D1RenderTarget&gt; pRT;
      CComPtr&lt;IWICBitmap&gt; pB;
      CComPtr&lt;IWICBitmapEncoder&gt; pEncoder;
      CComPtr&lt;IWICBitmapFrameEncode&gt; pFrame;
      CComPtr&lt;IWICStream&gt; pStream;
  
      WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
      HRESULT Hr = m_pWICFactory-&gt;CreateBitmap(pBit-&gt;GetSize().width,pBit-&gt;GetSize().height,format,WICBitmapCacheOnLoad,&amp;pB); 
  
      if (SUCCEEDED(Hr)) {
          D2D1_RENDER_TARGET_PROPERTIES RTProps = RenderTargetProperties();
          RTProps.pixelFormat = PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED);
          Hr = m_pDirect2dFactory-&gt;CreateWicBitmapRenderTarget(pB,&amp;RTProps,&amp;pRT);
      }
  
      if (SUCCEEDED(Hr)) {
          pRT-&gt;BeginDraw();
          pRT-&gt;Clear();
          pRT-&gt;DrawBitmap(pBit);
          Hr = pRT-&gt;EndDraw();
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = m_pWICFactory-&gt;CreateStream(&amp;pStream);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pStream-&gt;InitializeFromFilename(Path,GENERIC_WRITE);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = m_pWICFactory-&gt;CreateEncoder(GUID_ContainerFormatPng,NULL,&amp;pEncoder);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pEncoder-&gt;Initialize(pStream,WICBitmapEncoderNoCache);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pEncoder-&gt;CreateNewFrame(&amp;pFrame,NULL);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pFrame-&gt;Initialize(NULL);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pFrame-&gt;SetSize(pBit-&gt;GetSize().width,pBit-&gt;GetSize().height);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pFrame-&gt;SetPixelFormat(&amp;format);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pFrame-&gt;WriteSource(pB,NULL);
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pFrame-&gt;Commit();
      }
  
      if (SUCCEEDED(Hr)) {
          Hr = pEncoder-&gt;Commit();
      }
  }
  </code></pre>
  
  <p>I understand that you can't use resources made by another factory with another, but there has to be a way to make this work.</p>