Rendering part of a bitmap using Direct2D

北城以北 提交于 2019-12-24 14:30:30

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7759803/rendering-part-of-a-bitmap-using-direct2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!