Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct2D bitmap brush elongated

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);
}
like image 441
Nick Avatar asked Oct 15 '25 18:10

Nick


1 Answers

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.

like image 197
Anton Angelov Avatar answered Oct 18 '25 06:10

Anton Angelov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!