title: ID2D1HwndRenderTarget::CreateBitmapFromWicBitmap()
body: <p>I am trying to use WIC to load picture from the file and display it on screen with Direct2D. I follow the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd756686(v=vs.85).aspx" rel="nofollow">MSDN example</a> but I encounter a problem with the function CreateBitmapFromWicBitmap(). </p>
  
  <p>No matter which combination of <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd756766(v=vs.85).aspx#supported_wic_formats" rel="nofollow">pixel formats</a> I use during <code>ID2D1HwndRenderTarget</code> creation and <code>IWICFormatConverter::Initialize()</code> function calling, function <code>CreateBitmapFromWicBitmap()</code> returns 0x88982f80  error (<code>WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT</code>).</p>
  
  <p>I call the function using the same <code>ID2D1HwndRenderTarget</code> which I use for drawing. Should I create another render target? </p>
  
  <p>In the comment section in this <a href="https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/416004c5-6b73-45cd-a820-92b6608e9bfd/id2d1rendertargetcreatebitmap-returns-0x88982f80?forum=windowssdk" rel="nofollow">link</a>, someone wrote that <code>CreateBitmapFromWicBitmap()</code> should be called by DXGI surface render target. Does it mean that this function simply cannot be used with <code>ID2D1HwndRenderTarget</code>?</p>
  
  <p>EDIT:</p>
  
  <pre><code>void LoadBitmapFromFile(ID2D1HwndRenderTarget* target, ID2D1Bitmap** ppBitmap)
  {
      IWICImagingFactory* factory;
      IWICBitmapDecoder* decoder;
      IWICBitmapFrameDecode* frame;
      IWICFormatConverter* converter;
  
      CoInitializeEx(0, COINIT_MULTITHREADED);
      CoCreateInstance(CLSID_WICImagingFactory1,
                       NULL,
                       CLSCTX_INPROC_SERVER,
                       IID_IWICImagingFactory,
                       reinterpret_cast&lt;void**&gt;(&amp;factory));
  
      factory-&gt;CreateDecoderFromFilename(L".png",
                                         NULL,
                                         GENERIC_READ,
                                         WICDecodeMetadataCacheOnLoad,
                                         &amp;decoder);
  
      decoder-&gt;GetFrame(0, &amp;frame);
  
      factory-&gt;CreateFormatConverter(&amp;converter);
  
      converter-&gt;Initialize(frame,
                            GUID_WICPixelFormat32bppPBGRA,
                            WICBitmapDitherTypeNone,
                            NULL,
                            0.f,
                            WICBitmapPaletteTypeMedianCut);
  
      target-&gt;CreateBitmapFromWicBitmap(frame, 0, ppBitmap);
  }
  </code></pre>
  
  <p>ID2D1HwndRenderTarget is created like that:</p>
  
  <pre><code>ID2D1Factory* factory;
  ID2D1HwndRenderTarget* target;
  
  D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &amp;factory);
  
  factory-&gt;CreateHwndRenderTarget(
      D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_HARDWARE,
                                   D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)), 
      D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(width, height)),
      &amp;target);
  </code></pre>