使用 Direct2D 渲染位图的一部分
我正在将自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 direct2d 专家(direct3d),我在查看您的源代码和文档时发现,您传递了 DrawBitmap 的第二个参数 - NULL,而不是提供位图的源矩形,
我建议您查看 此处 例子
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
I would suggest looking here for example
抱歉给您带来麻烦 - 上面的代码工作正常。问题出在我的代码库的其他地方。
不知道如何将这个问题标记为“没关系”,但这很可能应该是这样。
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.