ID2D1DeviceContext EndDraw D2DERR_WRONG_STATE after drawing rectangle

1

I try to draw a rectangle using Direct2D. After initializing Direct2D device and Direct2D device context and setting up the render target, described in the following MSDN article, I tried to draw a rectangle as follows:

  HRESULT result;
  result = g_d2dContext->CreateSolidColorBrush(
    D2D1::ColorF(D2D1::ColorF::Blue),
    &g_SolidBrush
    );

  D2D1_RECT_F rect = D2D1::RectF(
    g_targetRect.left + 10.0f,
    g_targetRect.top + 10.0f,
    g_targetRect.right - 10.0f,
    g_targetRect.bottom - 10.0f);

  g_d2dContext->BeginDraw();
  {
    g_d2dContext->DrawRectangle(rect, g_SolidBrush, 5.0f);
  }
  result = g_d2dContext->EndDraw();

  if (FAILED(result))
  {
    OutputDebugStringW(L"The object was not in the correct state to process the method.");
  }

The EndDraw() method returns the following HRESULT: 0x88990001 (D2DERR_WRONG_STATE) - The object was not in the correct state to process the method.

The rectangle will not be drawn. I only get a black image.

EDIT:

I suppose that the issue is that a get a NULL pointer to the bitmap/render target after calling CreateBitmapFromDxgiSurface.

  g_d2dContext->CreateBitmapFromDxgiSurface(
    dxgiBackBuffer.Get(),
    &bitmapProperties,
    &g_targetBitmap);

  g_d2dContext->SetTarget(g_targetBitmap.Get());

But why targetBitmap is NULL?

Any ideas?

Thanks in advance.

c++
directx
direct2d
hresult
drawrectangle
asked on Stack Overflow Sep 29, 2014 by n2k • edited Sep 29, 2014 by n2k

1 Answer

0

I just solved the problem. I initalized the bitmapProperties with the wrong values.

Now it works:

  D2D1_BITMAP_PROPERTIES1 bitmapProperties =
    D2D1::BitmapProperties1(
    D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
    D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
    96.0f, // dpi
    96.0f  // dpi
    );

  // Direct2D needs the dxgi version of the backbuffer surface pointer.
  ComPtr<IDXGISurface> dxgiBackBuffer;
  hr = g_swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer));

  // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
  hr = g_d2dContext->CreateBitmapFromDxgiSurface(
    dxgiBackBuffer.Get(),
    bitmapProperties,
    g_targetBitmap.GetAddressOf());

  // Now we can set the Direct2D render target.
  g_d2dContext->SetTarget(g_targetBitmap.Get());
answered on Stack Overflow Sep 29, 2014 by n2k

User contributions licensed under CC BY-SA 3.0