title: Can't draw compatible target bitmap as effect to this compatible target
body: <p>I want is to create backdrop-blur effect as in UWP apps in-app blur for my ui library, but I've been stuck with it for a few days.</p>
  <p>For some reason, it either does not set backbuffer bitmap in SetInput method correctly, or in DrawImage call. The saddest thing is that backbuffer bitmap is actually valid, it could be drawn by same DrawImage call. Maybe I should specify another bitmap options when create it?</p>
  <p>My current code:</p>
  <pre><code>#include &lt;Windows.h&gt;
  
  HDC hdcDevice = GetDC(NULL);
  int xw = GetDeviceCaps(hdcDevice, HORZRES);
  int yw = GetDeviceCaps(hdcDevice, VERTRES);
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
  
  HWND hwnd;
  
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;memory.h&gt;
  #include &lt;wchar.h&gt;
  #include &lt;math.h&gt;
  
  #include &lt;d2d1_1.h&gt;
  #include &lt;d2d1helper.h&gt;
  #include &lt;dwrite.h&gt;
  #include &lt;wincodec.h&gt;
  
  #pragma comment(lib, &quot;d2d1&quot;)
  #pragma comment(lib, &quot;dxguid.lib&quot;)
  
  template&lt;class Interface&gt;
  inline void SafeRelease(
      Interface** ppInterfaceToRelease)
  {
      if (*ppInterfaceToRelease != NULL)
      {
          (*ppInterfaceToRelease)-&gt;Release();
          (*ppInterfaceToRelease) = NULL;
      }
  }
  
  #ifndef Assert
  #if defined( DEBUG ) || defined( _DEBUG )
  #define Assert(b) do {if (!(b)) {OutputDebugStringA(&quot;Assert: &quot; #b &quot;\n&quot;);}} while(0)
  #else
  #define Assert(b)
  #endif //DEBUG || _DEBUG
  #endif
  
  #ifndef HINST_THISCOMPONENT
  EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  #define HINST_THISCOMPONENT ((HINSTANCE)&amp;__ImageBase)
  #endif
  
  ID2D1Factory* m_pDirect2dFactory;
  ID2D1HwndRenderTarget* m_pRenderTarget;
  ID2D1DeviceContext* target;
  ID2D1SolidColorBrush* brush;
  
  void Release()
  {
        SafeRelease(&amp;m_pRenderTarget);
        SafeRelease(&amp;target);
        SafeRelease(&amp;brush);
  }
  
  void Init()
  {
  Release();
  m_pRenderTarget = NULL;
  
  
  RECT rc;
  GetClientRect(hwnd, &amp;rc);
  
  D2D1_SIZE_U size = D2D1::SizeU(
  rc.right - rc.left,
  rc.bottom - rc.top);
  
  // Create a Direct2D render target.
  SUCCEEDED(m_pDirect2dFactory-&gt;CreateHwndRenderTarget(
  D2D1::RenderTargetProperties(),
  D2D1::HwndRenderTargetProperties(hwnd, size),
  &amp;m_pRenderTarget));
  
  m_pRenderTarget-&gt;QueryInterface(&amp;target);
  }
  
  
  inline void Blur(ID2D1DeviceContext* backTarget, int rad, RECT r)
  // r is not used, should contain element bound box in future,
  // so backtarget-&gt;DrawImage will only draw part that 
  // belongs to element's area
  {
  RECT rc;
  GetClientRect(hwnd, &amp;rc);
  
  D2D1_SIZE_U size = D2D1::SizeU(
  rc.right - rc.left,
  rc.bottom - rc.top);
  
  // Draw rectangle for test
  backTarget-&gt;FillRectangle(D2D1::RectF(30, 30, 100, 100), brush);
  
  ID2D1Bitmap1* bb = nullptr;
  
  // Create bitmap
  backTarget-&gt;CreateBitmap(size, 0, 0, D2D1::BitmapProperties1(
  D2D1_BITMAP_OPTIONS_TARGET,
  D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)
  ), &amp;bb);
  
  // Copy current taget's state to created bitmap
  bb-&gt;CopyFromRenderTarget(0, backTarget, 0);
  
  
  ID2D1Effect* blur = nullptr;
  target-&gt;CreateEffect(CLSID_D2D1GaussianBlur, &amp;blur);
  blur-&gt;SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, 10);
  blur-&gt;SetInput(0, bb);
  
  // Draw blurred result. Does nothing
  backTarget-&gt;DrawImage(blur);
  
  // Just test if bb is valid, draw
  // it with some offset. 
  // Draws correctly
  auto a = D2D1::Point2F(100, 0);
  backTarget-&gt;DrawImage(bb, a);
  
  SafeRelease(&amp;blur);
  }
  
  inline void Render()
  {
  RECT rc;
  GetClientRect(hwnd, &amp;rc);
  
  D2D1_SIZE_U size = D2D1::SizeU(
  rc.right - rc.left,
  rc.bottom - rc.top);
  
  ID2D1BitmapRenderTarget* tar = nullptr; // Create back buffer
  target-&gt;CreateCompatibleRenderTarget(&amp;tar);
  
  ID2D1DeviceContext* tt = nullptr; 
  // Get exactly back buffer as ID2D1DeviceContext*, 
  // because it has more draw call, such as DrawImage()
  tar-&gt;QueryInterface(&amp;tt);
  tt-&gt;CreateSolidColorBrush(
  D2D1::ColorF(255, 0, 0),
  &amp;brush
  );
  
  
  tt-&gt;BeginDraw();
  tt-&gt;Clear(D2D1::ColorF(D2D1::ColorF::Blue));
  tt-&gt;SetTransform(D2D1::Matrix3x2F::Identity());
  
  // for(int i = 0; i &lt; ui_elements_count; i++)
  // {
  //     ui_element-&gt;DrawBlurredAreaBehindIt(tt);
  //     ui_element-&gt;DrawInnerText(tt);
  //     ui_element-&gt;DrawBorder(tt);
  //     ui_element-&gt;DrawSomethingElse(tt);
  // }
  // loop through ui elements should be here, 
  // assume we have an element with blur needed
  Blur(tt, 10, RECT());
  
  tt-&gt;EndDraw();
  
  target-&gt;BeginDraw();
  
  ID2D1Bitmap* bmp = nullptr;
  tar-&gt;GetBitmap(&amp;bmp);
  
  target-&gt;DrawImage(bmp);
  
  SafeRelease(&amp;tar);
  SafeRelease(&amp;tt);
  SafeRelease(&amp;bmp);
  SafeRelease(&amp;brush);
  
  target-&gt;EndDraw();
  }
  
  
  
  int WINAPI WinMain(HINSTANCE hin, HINSTANCE, LPSTR, int)
  {
  ReleaseDC(NULL, hdcDevice);
  
  WNDCLASS c = { NULL };
  c.lpszClassName = L&quot;asd&quot;;
  c.lpfnWndProc = WndProc;
  c.hInstance = hin;
  c.style = CS_VREDRAW | CS_HREDRAW;
  c.hCursor = LoadCursor(NULL, IDC_ARROW);
  c.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
  RegisterClass(&amp;c);
  
  int cx = 500, cy = 500;
  int x = xw / 2 - cx / 2, y = yw / 2 - cy / 2;
  
  hwnd = CreateWindowEx(NULL, L&quot;asd&quot;, L&quot;asd&quot;, WS_POPUP | WS_VISIBLE, x, y, cx, cy, NULL, NULL, hin, 0);
  
  HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
  CoInitialize(NULL);
  
  SUCCEEDED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &amp;m_pDirect2dFactory));
  
  Init();
  
  MSG msg;
  
  while (GetMessage(&amp;msg, NULL, 0, 0))
  {
  Render();
  TranslateMessage(&amp;msg);
  DispatchMessage(&amp;msg);
  }
  
  CoUninitialize();
  
  return 0;
  }
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
  {
  switch (message)
  {
  default:
  return DefWindowProc(hwnd, message, wp, lp);
  }
  return NULL;
  }
  </code></pre>
  <p>To obtain current drawing bitmap <code>ID2D1BitmapRenderTarget*</code> need to be created as backbuffer. But, this interface does not have all draw methods suchs as <code>DrawImage</code>, or <code>CreateEffect</code>, so I have tried to <code>QueryInterface</code> <code>ID2D1DeviceContext</code> from it, and it actually works.</p>
  <p>For getting backbuffer bitmap I use <code>ID2D1Bitmap::CopyFromRenderTarget</code> because if to draw bitmap from <code>ID2D1BitmapRenderTarget::GetBitmap</code> it will draw just nothing.</p>
  <p><strong>Important update</strong></p>
  <p>I just changed blur effect to scale effect, and fortunately or unfortunately, with scale effect it works. Please, don't tell it is a direct2d bug, I sure I doing here something wrong.</p>
  <p><a href="https://i.stack.imgur.com/MkK4i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MkK4i.png" alt="After scale" /></a></p>
  <p>I noticed that in all examples this effect created and initialized by <code>SetInput</code> and <code>SetValue</code> calls <strong>before</strong> the render loop, out of <code>BeginDraw</code> and <code>EndDraw</code>. Maybe after that calls the image is somehow prepared asynchronously, so it just does not have time be ready to be drawn being in render loop? But it sounds crazy.</p>