ID2D1RenderTarget::CreateBitmap Method

Syntax

HRESULT CreateBitmap(D2D1_SIZE_U size, const void *srcData, UINT32 pitch, const D2D1_BITMAP_PROPERTIES &bitmapProperties, ID2D1Bitmap **bitmap);

Creates a bitmap that is compatible with this render target.

Parameters

Name Type Description
size D2D1_SIZE_U The dimensions of the bitmap in pixels.
srcData const void * A pointer to the bitmap data. This pointer can be NULL. If it is NULL, the bitmap is initialized with zeros.
pitch UINT32 The size of each row of the bitmap data in bytes. This value must be a multiple of 4 bytes.
bitmapProperties const D2D1_BITMAP_PROPERTIES & The properties of the bitmap, such as the pixel format and DPI.
bitmap ID2D1Bitmap ** When this method returns, contains a pointer to a new bitmap object.

Return Value

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

You can create a bitmap using this method and then draw it to the render target. The bitmap created by this method is compatible with the render target; that is, it uses the same memory space and has the same pixel format.

If you specify NULL for srcData, the bitmap is initialized with zeros. The pitch is the number of bytes between the beginning of one row and the beginning of the next row in the source data.

The D2D1_BITMAP_PROPERTIES structure specifies the pixel format and the dots per inch (DPI) of the bitmap. You can use the GetPixelFormatData method of the ID2D1Factory interface to retrieve information about pixel formats.

Requirements

Component Version
Minimum supported client Windows 7, Windows Vista with SP1 and Platform Update
Minimum supported server Windows Server 2008 R2, Windows Server 2008 with SP1 and Platform Update
Header d2d1.h
Library d2d1.lib
DLL d2d1.dll

Example

The following example creates a bitmap from raw pixel data. This is a simplified example and might require adjustments based on your specific pixel format and data layout.

C++
// Assume pRenderTarget is a valid ID2D1RenderTarget* // Assume pFactory is a valid ID2D1Factory* UINT32 width = 100; UINT32 height = 50; UINT32 pitch = width * sizeof(D2D1_COLOR_F); // Assuming RGBA floats // Simple RGBA color data (e.g., all red) std::vector<D2D1_COLOR_F> pixelData(width * height, D2D1_COLOR_F{1.0f, 0.0f, 0.0f, 1.0f}); D2D1_BITMAP_PROPERTIES bitmapProperties = D2D1::BitmapProperties( D2D1::PixelFormat(DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED) ); ID2D1Bitmap *pBitmap = nullptr; HRESULT hr = pRenderTarget->CreateBitmap( D2D1::SizeU(width, height), pixelData.data(), pitch, bitmapProperties, &pBitmap ); if (SUCCEEDED(hr)) { // Use the pBitmap here, e.g., draw it to the render target // pRenderTarget->DrawBitmap(pBitmap); // Remember to release pBitmap when done // pBitmap->Release(); }

Related Topics