title: Wrong render target ID2D1Bitmap
body: <p>I'm trying to save a ID2D1Bitmap based on this <a href="https://stackoverflow.com/questions/11447489/how-to-save-id2d1bitmap-to-png-file">How to save ID2D1Bitmap to PNG file</a> but unlike him at hr=pRT2->EndDraw(); my hr =-2003238891. which means D2DERR_WRONG_RESOURCE_DOMAIN. how do i use the source render target (pRenderTarget) to fix this?</p>
  
  <p><strong>bitmapToFile.cpp</strong></p>
  
  <pre><code>HRESULT bitmapToFile::SaveBitmapToFile(PCWSTR uri,ID2D1Bitmap* pBitmap,ID2D1RenderTarget* pRenderTarget)
  {
      HRESULT hr = S_OK;
  
      IWICBitmap *pWICBitmap = NULL;
      ID2D1RenderTarget *pRT2 = NULL;
      IWICBitmapEncoder *pEncoder = NULL;
      IWICBitmapFrameEncode *pFrameEncode = NULL;
      IWICStream *pStream = NULL;
  
      if (SUCCEEDED(hr))
      {
          // Create a WIC factory.
          CoCreateInstance(
              CLSID_WICImagingFactory,
              NULL,
              CLSCTX_INPROC_SERVER,
              IID_IWICImagingFactory,
              reinterpret_cast&lt;void **&gt;(&amp;m_pWICFactory)
              );
      }
  
      UINT sc_bitmapWidth = pBitmap-&gt;GetSize().width;
      UINT sc_bitmapHeight = pBitmap-&gt;GetSize().height;
  
      if (SUCCEEDED(hr))
      {
          hr = m_pWICFactory-&gt;CreateBitmap(
              sc_bitmapWidth,
              sc_bitmapHeight,
              GUID_WICPixelFormat32bppPBGRA,
              WICBitmapCacheOnLoad,
              &amp;pWICBitmap
              );
      }
  
      if (SUCCEEDED(hr))
      {
  
          D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
          rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
          rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
          rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;
  
          hr = m_pD2DFactory2-&gt;CreateWicBitmapRenderTarget(
              pWICBitmap,
              rtProps,
              &amp;pRT2
              );
      }
  
      if (SUCCEEDED(hr))
      {
          pRT2-&gt;BeginDraw();
  
          pRT2-&gt;Clear();
  
          pRT2-&gt;DrawBitmap(pBitmap);
  
          hr=pRT2-&gt;EndDraw();
      }
      if (SUCCEEDED(hr))
      {
          hr = m_pWICFactory-&gt;CreateStream(&amp;pStream);
      }
  
      WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
      if (SUCCEEDED(hr))
      {
  
          hr = pStream-&gt;InitializeFromFilename(uri, 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;pFrameEncode, NULL);
      }
      if (SUCCEEDED(hr))
      {
          hr = pFrameEncode-&gt;Initialize(NULL);
      }
  
      if (SUCCEEDED(hr))
      {
          hr = pFrameEncode-&gt;SetSize(sc_bitmapWidth, sc_bitmapHeight);
      }
      if (SUCCEEDED(hr))
      {
          hr = pFrameEncode-&gt;SetPixelFormat(&amp;format);
      }
      if (SUCCEEDED(hr))
      {
          hr = pFrameEncode-&gt;WriteSource(pWICBitmap, NULL);
      }
      if (SUCCEEDED(hr))
      {
          hr = pFrameEncode-&gt;Commit();
      }
      if (SUCCEEDED(hr))
      {
          hr = pEncoder-&gt;Commit();
      }
  
      return hr;
  }
  </code></pre>