Bitmaps in Direct2D
A bitmap in Direct2D represents pixel data that can be used as a source for drawing operations. Bitmaps enable you to render images, create custom textures, and perform off‑screen rendering.
Key Concepts
- Bitmap Types – Target, CPU‑accessible, and GPU‑shared.
- Pixel Formats – DXGI formats and alpha modes.
- Bitmap Properties – Size, DPI, and color space.
- Interop – Sharing with Direct3D, WIC, and Windows Imaging.
Creating a Bitmap
HRESULT CreateBitmap(
ID2D1RenderTarget *renderTarget,
const D2D1_SIZE_U &size,
const D2D1_BITMAP_PROPERTIES &props,
ID2D1Bitmap **bitmap)
{
return renderTarget->CreateBitmap(size, nullptr, 0, &props, bitmap);
}
Bitmap Properties Structure
D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED),
96.0f, // DPI X
96.0f // DPI Y
);
Common Use Cases
| Scenario | Method | Notes |
|---|---|---|
| Loading from file | IWICImagingFactory + CreateBitmapFromWicBitmap | Supports PNG, JPEG, GIF, etc. |
| Off‑screen rendering | RenderTarget::CreateCompatibleRenderTarget | Use bitmap as the target. |
| Dynamic textures | Bitmap::CopyFromMemory | Update pixel data each frame. |
| Interoperability with Direct3D | DXGI Surface + ID2D1Bitmap1 | Share resources across APIs. |
Performance Tips
- Prefer GPU‑only bitmaps for static images.
- Use
D2D1_BITMAP_OPTIONS_CPU_READonly when necessary. - Match bitmap DPI to the render target to avoid scaling.
- Reuse bitmap objects instead of recreating them.