使用Direct2D渲染位图的一部分



我正在将自定义CAD软件从GDI转换为Direct2D。我在平移绘图时遇到了问题。我要做的是创建一个位图它的宽度是绘图窗口的3倍3倍高。然后,当用户开始平移时,我会渲染位图中应该可见的部分。

问题是,它看起来好像你不能有比你的渲染目标更大的位图。以下是我目前所做的大致内容:

// Get the size of my drawing window.
RECT rect;
HDC hdc = GetDC(hwnd);
GetClipBox(hdc, &rect);
D2D1_SIZE_U size = D2D1::SizeU(
    rect.right - rect.left,
    rect.bottom - rect.top
);
// Now create the render target
ID2D1HwndRenderTarget *hwndRT = NULL;
hr = m_pD2DFactory->CreateHwndRenderTarget(
    D2D1::RenderTargetProperties(),
    D2D1::HwndRenderTargetProperties(hwnd, size),
    &hwndRT
    );
// And then the bitmap render target
ID2D1BitmapRenderTarget *bmpRT = NULL;
// We want it 3x as wide & 3x as high as the window
D2D1_SIZE_F size = D2D1::SizeF(
    (rect.right - rect.left) * 3, 
    (rect.bottom - rect.top) * 3
);
hr = originalTarget->CreateCompatibleRenderTarget(
        size,
        &bmpRT
        );
// Now I draw the geometry to my bitmap Render target...
// Then get the bitmap
ID2D1Bitmap* bmp = NULL;
bmpRT->GetBitmap(&bmp);
// From here I want to draw that bitmap on my hwndRenderTarget.
// Based on where my mouse was when I started panning, and where it is
// now, I can create a destination rectangle. It's the size of my
// drawing window
D2D1_RECT_U dest = D2D1::RectU(x1, y1, x1+size.width, y1+size.height);
hwndRT->DrawBitmap(
    bmp,
    NULL,
    1.0,
    D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
    dest
    );

当我检查位图的大小时,它会检查OK -这是我的位图渲染目标的大小,而不是我的hwnd渲染目标。但如果我设x1 &Y1到0,它应该绘制位图的左上角(这是屏幕外的一些几何图形)。但是它只是在屏幕上绘制的左上角

有人有这方面的经验吗?我如何创建一个相当大的位图,然后在较小的渲染目标上渲染它的一部分?因为我在平移,渲染将发生在每次鼠标移动,所以它必须是合理的性能。

我不是direct2d专家(direct3d),我发现查看你的源代码和文档是你传递DrawBitmap的第二个参数- NULL,而不是提供你的位图的源矩形

    m_pRenderTarget->DrawBitmap(
        m_pBitmap,
        D2D1::RectF(
            upperLeftCorner.x,
            upperLeftCorner.y,
            upperLeftCorner.x + scaledWidth,
            upperLeftCorner.y + scaledHeight),
        0.75,
        D2D1_BITMAP_INTERPOLATION_MODE_LINEAR
        );

我建议看这里的例子

很抱歉给您带来麻烦-上面的代码工作正常。问题在我的代码库的其他地方。

不知道如何将这个问题标记为"Nevermind",但这可能是它应该的。

最新更新