2015-03-02 84 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來調整拉伸質量(算法)。我認爲它默認爲最近的鄰居,但線性插值質量更好。