ID2D1Bitmap Interface
The ID2D1Bitmap interface represents a bitmap that can be used as a source for drawing operations. It encapsulates pixel data and provides methods for accessing bitmap properties and copying pixel data.
Namespace: Microsoft::DirectX::Direct2D
Inheritance: ID2D1Resource > ID2D1Bitmap
Properties
| Property | Type | Description |
|---|---|---|
PixelSize | D2D1_SIZE_U | Width and height of the bitmap in pixels. |
PixelFormat | D2D1_PIXEL_FORMAT | Format of the pixels stored in the bitmap. |
DpiX | FLOAT | Horizontal DPI (dots per inch). |
DpiY | FLOAT | Vertical DPI (dots per inch). |
Methods
| Method | Signature | Description |
|---|---|---|
GetSize |
void GetSize(D2D1_SIZE_F *size) const |
Retrieves the size of the bitmap in device-independent pixels. |
CopyFromBitmap |
HRESULT CopyFromBitmap(const D2D1_POINT_2U *destPoint, ID2D1Bitmap *srcBitmap, const D2D1_RECT_U *srcRect) |
Copies pixel data from another bitmap. |
CopyFromRenderTarget |
HRESULT CopyFromRenderTarget(const D2D1_POINT_2U *destPoint, ID2D1RenderTarget *renderTarget, const D2D1_RECT_U *srcRect) |
Copies pixel data from a render target. |
CopyFromMemory |
HRESULT CopyFromMemory(const D2D1_RECT_U *dstRect, const void *srcData, UINT32 pitch) |
Copies pixel data from system memory into the bitmap. |
GetPixelData |
HRESULT GetPixelData(UINT32 options, D2D1_MAPPED_RECT *mappedRect) |
Maps the bitmap memory for CPU read/write. |
Example Usage
The following C++ example creates a bitmap, fills it with a solid color, and draws it onto a render target.
#include <d2d1.h>
#pragma comment(lib, "d2d1.lib")
void DrawBitmap(ID2D1RenderTarget* pRT)
{
// Create a bitmap properties object
D2D1_BITMAP_PROPERTIES bmpProps = D2D1::BitmapProperties(
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED),
96.0f, 96.0f);
// Define bitmap size (200x200)
D2D1_SIZE_U size = D2D1::SizeU(200, 200);
// Create the bitmap
ID2D1Bitmap* pBitmap = nullptr;
HRESULT hr = pRT->CreateBitmap(size, nullptr, 0, &bmpProps, &pBitmap);
if (SUCCEEDED(hr))
{
// Fill the bitmap with a solid red color
D2D1_COLOR_F red = D2D1::ColorF(D2D1::ColorF::Red);
D2D1_RECT_U rect = D2D1::RectU(0, 0, size.width, size.height);
UINT32 stride = size.width * 4; // 4 bytes per pixel
std::vector<UINT32> pixels(size.width * size.height, 0xFF0000FF); // ARGB
pBitmap->CopyFromMemory(&rect, pixels.data(), stride);
// Draw the bitmap at (50,50)
pRT->DrawBitmap(pBitmap,
D2D1::RectF(50.f, 50.f, 250.f, 250.f),
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
nullptr);
}
if (pBitmap) pBitmap->Release();
}