title: Using CreateSharedBitmap method to create a bitmap
body: <p>Can you use CreateSharedBitmap method to create a shared bitmap without IDXGIDevice ?
  my src render target is a hwndrendertarget. 
  Basically what I want to do is to save a image from a hwndrendertarget. 
  This is what i have tried. </p>
  
  <p>Thanks</p>
  
  <pre><code>HRESULT bitmapToFile::SaveBitmapToFile(PCWSTR uri,ID2D1Bitmap* pBitmap,ID2D1RenderTarget* pRenderTarget)
  {
      HRESULT hr = S_OK;
      ID2D1Bitmap *m_pBitmap1= NULL;
      IWICBitmap *pWICBitmap = NULL;
      ID2D1RenderTarget *pRT = NULL;
      IWICBitmapEncoder *pEncoder = NULL;
      IWICBitmapFrameEncode *pFrameEncode = NULL;
      IWICStream *pStream = NULL;
  
  
      if (SUCCEEDED(hr))
      {
          // Create a WIC factory.
          hr=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))
      {
          WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
          hr = m_pWICFactory-&gt;CreateBitmap(
              sc_bitmapWidth,
              sc_bitmapHeight,
              format,
              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.usage = D2D1_RENDER_TARGET_USAGE_NONE;
          rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
          /*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;pRT
          );
  
          D2D1_BITMAP_PROPERTIES bp2 = D2D1::BitmapProperties();
          /*bp.pixelFormat = rtProps.pixelFormat;*/
  
          bp2.pixelFormat = PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
          bp2.dpiX = bp2.dpiY = 0.0f;
  
          hr=pRT-&gt;CreateSharedBitmap(
          __uuidof(IWICBitmap),
          static_cast&lt;void*&gt;(pWICBitmap),
          &amp;bp2,
          &amp;m_pBitmap1
          );  
  
          hr=m_pBitmap1-&gt;CopyFromRenderTarget(nullptr, pRenderTarget, nullptr);
      }
  </code></pre>