使用 Direct2D 渲染位图的一部分

发布于 2024-12-10 08:52:40 字数 1720 浏览 1 评论 0原文

我正在将自定义 CAD 软件从 GDI 转换为 Direct2D。我在平移绘图时遇到问题。我想要做的是创建一个位图,其宽度是绘图窗口的 3 倍,并且宽度是绘图窗口的 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
    );

因此,当我检查位图的大小时,它检查出正常 - 这是我的位图渲染目标的大小,而不是我的 hwnd 渲染目标。但如果我设置 x1 & y1 为 0,它应该绘制位图的左上角(这是屏幕外的一些几何图形)。但它只是绘制屏幕上内容的左上角。

有人有这方面的经验吗?如何创建一个相当大的位图,然后在较小的渲染目标上渲染它的一部分?由于我正在平移,渲染将在每次鼠标移动时进行,因此它必须具有相当的性能。

I'm in the process of converting a custom CAD software from GDI to Direct2D. I'm having issues when panning the drawing. What I would like to do is to create a bitmap that's 3x as wide as the drawing window & 3x as high. Then, when the user begins to pan, I would render the part of the bitmap that should be visible.

Trouble is, it doesn't appear as though you can have bitmaps bigger than your render target. Here's approximately what I've done so far:

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

So when I check the size of my bitmap, it checks out OK - it's the size of my bitmap render target, not my hwnd render target. But if I set x1 & y1 to 0, it should draw the top left-hand corner of the bitmap (which is some geometry off the screen). But it just draws the top left-corner of what is on the screen.

Does anyone have any experience with this? How can I create a fairly large bitmap, and then render a portion of it on a smaller-sized render target? Since I'm panning, the render will take place on every mouse-move, so it has to be reasonably performant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2024-12-17 08:52:40

我不是 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
        );

我建议您查看 此处 例子

I am not direct2d expert (direct3d), and what I have found looking into your source and documentation is that you pass second parameter of DrawBitmap - NULL, instead of supplying source rectangle of your bitmap

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

I would suggest looking here for example

囍笑 2024-12-17 08:52:40

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

不知道如何将这个问题标记为“没关系”,但这很可能应该是这样。

Sorry for the trouble - the code above works OK. The problem was elsewhere in my codebase.

Not sure how to mark this question as "Nevermind," but that's likely what it should be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文