【问题标题】:ID2D1DeviceContext EndDraw D2DERR_WRONG_STATE after drawing rectangle绘制矩形后 ID2D1DeviceContext EndDraw D2DERR_WRONG_STATE
【发布时间】:2014-09-30 04:44:08
【问题描述】:

我尝试使用 Direct2D 绘制一个矩形。在初始化 Direct2D 设备和 Direct2D 设备上下文并设置渲染目标后,描述如下MSDN article,我尝试绘制如下矩形:

  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.");
  }

EndDraw() 方法返回以下 HRESULT:0x88990001 (D2DERR_WRONG_STATE) - The object was not in the correct state to process the method

矩形不会被绘制。我只得到黑色图像。

编辑:

我想问题是在调用 CreateBitmapFromDxgiSurface 之后获得指向位图/渲染目标的 NULL 指针。

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

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

但是为什么 targetBitmap 是 NULL 呢?

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: c++ directx direct2d hresult drawrectangle


    【解决方案1】:

    我刚刚解决了这个问题。我用错误的值初始化了 bitmapProperties。

    现在可以了:

      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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多