Direct2D bitmap brush elongated

被刻印的时光 ゝ 提交于 2020-01-15 05:51:31

问题


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

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