3

I have a problem with Direct2D bitmap scaling. I loaded a bitmap from a file using that example, then I wanted to scale bitmap by myself (fit to view saving proportions, add a shadow effect…) but Direct2D automatically scales bitmap (e.g. while resizing a window) and I do not know how to prevent this behavior.

For example, after bitmap loaded into a small window (CView) it fills the entire window correctly (according OnDraw) and when I maximize it D2D stretch my bitmap with losing bitmap quality and finally the bitmap greatly exceeds borders of the window despite my OnDraw method.

void CWDCView::OnDraw(CDC* /*pDC*/)
{
    CWDCDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc) return;

    HRESULT hr = S_OK;

    ID2D1DeviceContext *deviceContext;
    pRenderTarget->QueryInterface(&deviceContext);  //ID2D1HwndRenderTarget* pRenderTarget

    RECT rc = {0,0,0,0};
    GetClientRect(&rc);

    deviceContext->BeginDraw();

    deviceContext->Clear( D2D1::ColorF( D2D1::ColorF(0xC8D2E1, 1.0f) ) );

    D2D1_RECT_F rect={0,0,rc.right,rc.bottom};
    deviceContext->DrawBitmap(m_pBitmap,rect); //ID2D1Bitmap *m_pBitmap

    deviceContext->EndDraw();

    if (hr == D2DERR_RECREATE_TARGET)
    {
        hr = S_OK;
        ReleaseDeviceResources();
    }

    SafeRelease(&deviceContext);
}

So how to prevent or turn off this “autoscaling”?

I want to add one thing. If draw bitmap like that deviceContext->DrawBitmap(m_pBitmap); /*without rect*/ it draws just a part of the bitmap without fitting it into the window but when maximizing it stretch it anyway.

Craftsmann
  • 95
  • 1
  • 10

1 Answers1

1

A render target could not be just automatically resized. In your case, you are changing the size of the window, but this doesn't cause a change of the "attached" render target size.

You should handle the resize event of your window and then you have two possibilities:

  1. Recreate your render target with the new size.
  2. Use ID2D1HwndRenderTarget::Resize or IDXGISwapChain::ResizeBuffers

Additionally, you can check:

Community
  • 1
  • 1
Peter Kostov
  • 941
  • 1
  • 6
  • 15