C++ 使用Direct2D渲染位图的一部分

C++ 使用Direct2D渲染位图的一部分,c++,direct2d,C++,Direct2d,我正在将自定义CAD软件从GDI转换为Direct2D。我在平移图形时遇到问题。我想做的是创建一个位图,它的宽度是绘图窗口的3倍,高度是3倍。然后,当用户开始平移时,我将渲染位图中应该可见的部分 问题是,它看起来并不像您可以拥有比渲染目标更大的位图。以下是我到目前为止所做的大致情况: // Get the size of my drawing window. RECT rect; HDC hdc = GetDC(hwnd); GetClipBox(hdc, &rect); D2D1_S

我正在将自定义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”,但这很可能是应该的。

谢谢您的建议和链接。我以前见过这个链接——第二个参数表示要用位图填充多少目标渲染目标。在我的例子中,我想用位图填充整个渲染目标,所以我将其保留为空。好的,它将与D2D1::RectF(0,0,size.width,size.height)相同;