问题
I have to draw my shapes on a offscreen bitmap, but I have a strange problem when I try to render my bitmap.
This is how the image should be displayed:
And this how I a see the bitmap:
Following is the code I use to create the bitmap brush:
const auto size = renderTarget->GetSize();
const auto pxSize = D2D1::SizeU(size.width * 4, size.height * 4);
ID2D1BitmapRenderTarget* compatibleRenderTarget;
HRESULT hr = renderTarget->CreateCompatibleRenderTarget(size, pxSize, &compatibleRenderTarget);
if (SUCCEEDED(hr))
{
// compute visible area and the current transformation matrix
const auto area = get_visible_area(renderTarget);
const auto transform = D2D1::Matrix3x2F::Identity();
// offscreen bitmap rendering
compatibleRenderTarget->BeginDraw();
// draw all shapes
compatibleRenderTarget->EndDraw();
// Retrieve the bitmap from the render target.
ID2D1Bitmap* bitmap;
hr = compatibleRenderTarget->GetBitmap(&bitmap);
// release the compatible render target
compatibleRenderTarget->Release();
// Create the bitmap brush
ID2D1BitmapBrush* bitmapBrush = nullptr;
hr = renderTarget->CreateBitmapBrush(bitmap, D2D1::BitmapBrushProperties(), &bitmapBrush);
bitmap->Release();
// draw bitmap
renderTarget->FillRectangle(area, bitmapBrush);
}
回答1:
The effect is a result of a standard behavior. If you use bitmap brush you can choose different extend modes (default is clamp). This defines what will happen if the geometry size exceeds the bitmap size (as in your case with FillRect()). You have to define extend modes for X and Y axises in the D2D1_BITMAP_BRUSH_PROPERTIES structure which you are passing to ID2D1RenderTarget::CreateBitmapBrush().
You can choose between (as stated here):
- Clamp (repeat the last line of the bitmap)
- Wrap (tile the bitmap)
- Mirror
If you dont want your bitmap to be clamped neither wrapped, you can just use ID2D1RenderTarget::DrawBitmap() method instead.
Edit: If sourceRectangle differs from destinationRectangle in size, the bitmap will be stretched. You can adjust the stretch quality (algorithm) by specifying D2D1_BITMAP_INTERPOLATION_MODE. I think it defaults to nearest neighbour, but linear interpolation is better quality.
来源:https://stackoverflow.com/questions/28806029/direct2d-bitmap-brush-elongated