2015-03-02 83 views
2

我必须在离屏位图上绘制自己的形状,但是当我尝试渲染位图时出现了一个奇怪的问题。Direct2D位图画笔拉长

这是图像如何显示:

enter image description here

这我一个如何看待位:

enter image description here

以下是我用它来创建位图的代码刷子:

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); 
} 
+0

您的问题是关于扩展的底线,还是关于位图质量的损失? – 2015-03-02 13:00:57

+0

@AntonAngelov这是关于延长的底线。实际上,我通过兼容渲染目标的两倍来修复它,但我不能说为什么... – Nick 2015-03-02 16:17:10

+0

为什么不使用ID2D1RenderTarget :: DrawBitmap()并传递屏幕外位图,而不是创建位图刷?它更简单。另外第二个问题..你是否每次在HWND渲染目标上渲染时都重新绘制离屏位图? – 2015-03-02 16:31:07

回答

2

该效应是标准行为的结果。如果你使用位图刷,你可以选择不同的扩展模式(默认是钳位)。这定义了如果几何尺寸超过位图尺寸会发生什么(如您在FillRect()中的情况)。您必须在D2D1_BITMAP_BRUSH_PROPERTIES结构中为X轴和Y轴定义扩展模式,该结构将传递给ID2D1RenderTarget :: CreateBitmapBrush()

可以(as stated here)之间进行选择:

  • 夹(重复位图的最后一行)
  • 包裹(瓦位图)
  • 镜子

如果你不希望你的位图不被包装,你可以使用ID2D1RenderTarget :: DrawBitmap()方法。

编辑:如果sourceRectangledestinationRectangle不同的尺寸,所述位图将被拉伸。您可以通过指定D2D1_BITMAP_INTERPOLATION_MODE来调整拉伸质量(算法)。我认为它默认为最近的邻居,但线性插值质量更好。