title: Direct2D Loading and Drawing Bitmap
body: <p>I'm trying to draw a bitmap to the screen; the code that calls to load the bitmap from a file works, but it seems the actual drawing part is causing some program crashes. </p>
  
  <p>I followed a tutorial from MSDN, but the only change I made was instead of using ID2D1RenderTarget, I'm using a ID2D1DCRenderTarget.</p>
  
  <p>Here's the Load method that works:</p>
  
  <pre><code>HRESULT LoadBitmapFromFile(
      ID2D1DCRenderTarget *pRenderTarget,
      IWICImagingFactory *pIWICFactory,
      PCWSTR uri,
      UINT destinationWidth,
      UINT destinationHeight,
      ID2D1Bitmap **ppBitmap
      )
  {
      IWICBitmapDecoder *pDecoder = NULL;
      IWICBitmapFrameDecode *pSource = NULL;
      IWICStream *pStream = NULL;
      IWICFormatConverter *pConverter = NULL;
      IWICBitmapScaler *pScaler = NULL;
      HRESULT hr = pIWICFactory-&gt;CreateDecoderFromFilename(
          uri,
          NULL,
          GENERIC_READ,
          WICDecodeMetadataCacheOnLoad,
          &amp;pDecoder
          );
  
  
  
      if (SUCCEEDED(hr))
      {
          // Create the initial frame.
          hr = pDecoder-&gt;GetFrame(0, &amp;pSource);
      }
      if (SUCCEEDED(hr))
      {
  
          // Convert the image format to 32bppPBGRA
          // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
          hr = pIWICFactory-&gt;CreateFormatConverter(&amp;pConverter);
  
      }
  
  
      if (SUCCEEDED(hr))
      {
          // If a new width or height was specified, create an
          // IWICBitmapScaler and use it to resize the image.
          if (destinationWidth != 0 || destinationHeight != 0)
          {
              UINT originalWidth, originalHeight;
              hr = pSource-&gt;GetSize(&amp;originalWidth, &amp;originalHeight);
              if (SUCCEEDED(hr))
              {
                  if (destinationWidth == 0)
                  {
                      FLOAT scalar = static_cast&lt;FLOAT&gt;(destinationHeight) / static_cast&lt;FLOAT&gt;(originalHeight);
                      destinationWidth = static_cast&lt;UINT&gt;(scalar * static_cast&lt;FLOAT&gt;(originalWidth));
                  }
                  else if (destinationHeight == 0)
                  {
                      FLOAT scalar = static_cast&lt;FLOAT&gt;(destinationWidth) / static_cast&lt;FLOAT&gt;(originalWidth);
                      destinationHeight = static_cast&lt;UINT&gt;(scalar * static_cast&lt;FLOAT&gt;(originalHeight));
                  }
  
                  hr = pIWICFactory-&gt;CreateBitmapScaler(&amp;pScaler);
                  if (SUCCEEDED(hr))
                  {
                      hr = pScaler-&gt;Initialize(
                          pSource,
                          destinationWidth,
                          destinationHeight,
                          WICBitmapInterpolationModeCubic
                          );
                  }
                  if (SUCCEEDED(hr))
                  {
                      hr = pConverter-&gt;Initialize(
                          pScaler,
                          GUID_WICPixelFormat32bppPBGRA,
                          WICBitmapDitherTypeNone,
                          NULL,
                          0.f,
                          WICBitmapPaletteTypeMedianCut
                          );
                  }
              }
          }
          else // Don't scale the image.
          {
              hr = pConverter-&gt;Initialize(
                  pSource,
                  GUID_WICPixelFormat32bppPBGRA,
                  WICBitmapDitherTypeNone,
                  NULL,
                  0.f,
                  WICBitmapPaletteTypeMedianCut
                  );
          }
      }
      if (SUCCEEDED(hr))
      {
  
          // Create a Direct2D bitmap from the WIC bitmap.
          hr = pRenderTarget-&gt;CreateBitmapFromWicBitmap(
              pConverter,
              NULL,
              ppBitmap
              );
      }
  
      if (pDecoder) pDecoder-&gt;Release();
      if (pSource) pSource-&gt;Release();
      if (pStream) pStream-&gt;Release();
      if (pScaler) pScaler-&gt;Release();
  
      return hr;
  }
  </code></pre>
  
  <p>And in between the BeginDraw() and EndDraw() for the render target, I have the following code to draw the bitmap. This is the part that crashes the program when I attempt to run it: </p>
  
  <pre><code>d2dg-&gt;pRT-&gt;BeginDraw(); 
  
  // Create a WIC Factory
  HRESULT hr = CoCreateInstance(
  CLSID_WICImagingFactory,
  NULL,
  CLSCTX_INPROC_SERVER,
  IID_IWICImagingFactory,
  (LPVOID*)&amp;wicFactory);
  
  hr = LoadBitmapFromFile(d2dg-&gt;pRT, wicFactory, L"image1.png", 400, 400, &amp;pBitmap); 
  
  D2D1_SIZE_F size = pBitmap-&gt;GetSize();
  D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(100.f, 10.f);
  // Draw a bitmap.
  d2dg-&gt;pRT-&gt;DrawBitmap(
      pBitmap,
      D2D1::RectF(
      upperLeftCorner.x,
      upperLeftCorner.y,
      upperLeftCorner.x + size.width,
      upperLeftCorner.y + size.height)
      );
  
  d2dg-&gt;pRT-&gt;EndDraw();
  </code></pre>