How to Create a Bitmap Brush

To create a bitmap brush, use the ID2D1RenderTarget::CreateBitmapBrush method and specify the bitmap brush properties. Some overloads enable you to specify the brush properties. The following code shows how to create a bitmap brush to fill a square, and a solid black brush to draw the outline of the square. The code produces the output shown in the following screen shot.

Note

Starting with Windows 8, you can use the CreateBitmapBrush method on the ID2D1DeviceContext interface to create a ID2D1BitmapBrush1 instead of a ID2D1BitmapBrush. ID2D1BitmapBrush1 adds high-quality scaling modes to the bitmap brush.

 

screen shot of a square filled with a plant bitmap

  1. Declare a variable of type ID2D1BitmapBrush.

        ID2D1BitmapBrush *m_pBitmapBrush;
    
  2. Load a bitmap from a resource. For more information, see How to Load a Bitmap from a Resource.

    // Create the bitmap to be used by the bitmap brush.
    if (SUCCEEDED(hr))
    {
        hr = LoadResourceBitmap(
            m_pRenderTarget,
            m_pWICFactory,
            L"FERN",
            L"Image",
            &m_pBitmap
            );
    
  3. Choose the extend modes (D2D1_EXTEND_MODE) and interpolation mode (D2D1_BITMAP_INTERPOLATION_MODE) of the bitmap brush and then call the CreateBitmapBrush method to create a brush, as shown in the following code.

    hr = m_pRenderTarget->CreateBitmapBrush(
        m_pBitmap,
        &m_pBitmapBrush
        );
    

Direct2D Reference