3
votes

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:

enter image description here

And this how I a see the bitmap:

enter image description here

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
Is your question about the extended bottom line, or about the loss of quality of the bitmap?Anton Angelov
@AntonAngelov It's about the extended bottom line. Actually I fixed it by simply double the size of my compatible render target, but I can't say why...Nick
Why dont you use ID2D1RenderTarget::DrawBitmap() and pass the offscreen bitmap, instead of creating bitmap brush? It is far more simple. Also second question.. do you redraw the offscreen bitmap everytime you render on the HWND render target?Anton Angelov
@AntonAngelov I will try the DrawBitmap method, and yes, I do redraw the bitmap every time my render method is called (that is, every time I need to add a new "freehand drawing" shape, and unfortunately this happens very often since geometries are immutable objects). Is there a way to avoid the need to redraw the bitmap?Nick
If you redraw the offscreen bitmap everytime your render to the window then you loose ALL the benefit from using offscreen bitmap. It will make no sense. So you have to make separate functions (1.) to draw to the offscreen bitmap and (2.) function for drawing on the HWND render target. And call the first function (1) only when some of the shapes get changed and not everytime you render on the HWN RT. When drawing on the HWND RT, first calculate the visible area of the offscreen bitmap (only if you have zoomed), and then use DrawBitmap() to copy this particular area on the HWND RT.Anton Angelov

1 Answers

3
votes

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.