Skip to main content
Supported Pixel Formats and Alpha Modes

This topic describes the pixel formats and alpha modes supported by each render target type and the ID2D1Bitmap interface. It contains the following sections.

Specifying a Pixel Format for a Render Target

When you create a render target, you must specify its pixel format. To specify the pixel format, you use a D2D1_PIXEL_FORMAT structure to set the pixelFormat member of a D2D1_RENDER_TARGET_PROPERTIES structure. Then, you pass that structure to the appropriate Create method, such as ID2D1Factory::CreateHwndRenderTarget.

The D2D1_PIXEL_FORMAT structure has two fields:

  • format, a DXGI_FORMAT value that describes the size and arrangement of channels in each pixel, and
  • alpha, a D2D1_ALPHA_MODE value that describes how alpha information is interpreted.

The following example creates a D2D1_PIXEL_FORMAT structure and uses it to specify the pixel format and alpha mode of an ID2D1HwndRenderTarget.


RECT rc;
GetClientRect(m_hwnd, &rc);

D2D1_SIZE_U size = D2D1::SizeU(
    rc.right - rc.left,
    rc.bottom - rc.top
    );

// Create a pixel format and initial its format
// and alphaMode fields.
D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
    DXGI_FORMAT_B8G8R8A8_UNORM,
    D2D1_ALPHA_MODE_IGNORE
    );

D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties();
props.pixelFormat = pixelFormat;

// Create a Direct2D render target.
hr = m_pD2DFactory->CreateHwndRenderTarget(
    props,
    D2D1::HwndRenderTargetProperties(m_hwnd, size),
    &m_pRT
    );

Different render targets support different format and alpha mode combinations. The following sections list the format and alpha combinations supported by each render target.

Supported Formats for ID2D1HwndRenderTarget

The supported formats for an ID2D1HwndRenderTarget depend on whether it renders by using hardware or software, or whether Direct2D handles the rendering mode automatically by default.

Note  We recommend that you use DXGI_FORMAT_B8G8R8A8_UNORM as the pixel format for better performance. This is particularly helpful for software render targets. BGRA format targets perform better than RGBA formats.

When you create an ID2D1HwndRenderTarget, you use the D2D1_RENDER_TARGET_PROPERTIES structure to specify rendering options. The options include the pixel format, as noted in the previous section. The type field of this structure enables you to specify whether the render target renders to hardware or software, or whether Direct2D should automatically determine the rendering mode.

To enable Direct2D to determine whether the render target uses hardware or software rendering, use the D2D1_RENDER_TARGET_TYPE_DEFAULT setting.

The following table lists the supported formats for ID2D1HwndRenderTarget objects that are created by using the D2D1_RENDER_TARGET_TYPE_DEFAULT setting.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN

 

To force a render target to use hardware rendering, use the D2D1_RENDER_TARGET_TYPE_HARDWARE setting. The following table lists the supported formats for ID2D1HwndRenderTarget objects that explicitly use hardware rendering.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN

 

To force a render target to use software rendering, use the D2D1_RENDER_TARGET_TYPE_SOFTWARE setting. The following table lists the supported formats for ID2D1HwndRenderTarget objects that explicitly use software rendering.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN

 

Regardless of whether the ID2D1HwndRenderTarget is hardware accelerated, the DXGI_FORMAT_UNKNOWN format uses DXGI_FORMAT_B8G8R8A8 by default and the D2D1_ALPHA_MODE_UNKNOWN alpha mode uses D2D1_ALPHA_MODE_IGNORE by default.

Supported formats for ID2D1DeviceContext

Starting with Windows 8 the device context takes advantage of more of the Direct3D high color formats like:

  • DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
  • DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
  • DXGI_FORMAT_R16G16B16A16_UNORM
  • DXGI_FORMAT_R16G16B16A16_FLOAT
  • DXGI_FORMAT_R32G32B32A32_FLOAT

Use the ID2D1DeviceContext::IsDxgiFormatSupported method to see if a format works on a particular device context. These formats may also work on an ID2D1HwndRenderTarget.

These formats are in addition to the formats supported by the ID2D1HwndRenderTarget interface in Windows 7. See Devices and Device Contexts for more information.

Supported Formats for Compatible Render Target

A compatible render target (an ID2D1BitmapRenderTarget that is created by one of the ID2D1RenderTarget::CreateCompatibleRenderTarget methods) inherits the supported formats and alpha modes of the render target that created it. A compatible render target also supports the following format and alpha mode combinations, regardless of what its parent supports.

FormatAlpha mode
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_STRAIGHT
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN

 

The DXGI_FORMAT_UNKNOWN format uses the parent render target format by default and the D2D1_ALPHA_MODE_UNKNOWN alpha mode uses D2D1_ALPHA_MODE_PREMULTIPLIED by default.

Supported Formats for DXGI Surface Render Target

A DXGI render target is an ID2D1RenderTarget that is created by one of the ID2D1Factory::CreateDxgiSurfaceRenderTarget methods. It supports the following format and alpha mode combinations.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_STRAIGHT
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE

 

Note  The format must match the format of the DXGI surface to which the DXGI surface render target draws.

The DXGI_FORMAT_UNKNOWN format uses the DXGI surface format by default. Do not use the D2D1_ALPHA_MODE_UNKNOWN alpha mode with a DXGI surface render target. It has no default value and will cause the DXGI surface render target creation to fail.

Supported Formats for WIC Bitmap Render Target

A WIC bitmap render target is an ID2D1RenderTarget that is created by one of the ID2D1Factory::CreateWicBitmapRenderTarget methods. It supports the following format and alpha mode combinations.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_STRAIGHT
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN

 

The pixel format of the WIC bitmap target must match the pixel format of the WIC bitmap.

The DXGI_FORMAT_UNKNOWN format uses the WIC bitmap format by default and the D2D1_ALPHA_MODE_UNKNOWN alpha mode uses WIC bitmap alpha mode by default.

Supported Formats for ID2D1DCRenderTarget

An ID2D1DCRenderTarget supports the following format and alpha mode combinations.

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE

 

Do not use the DXGI_FORMAT_UNKNOWN format or the D2D1_ALPHA_MODE_UNKNOWN alpha mode with an ID2D1DCRenderTarget. It has no default value and will cause the ID2D1DCRenderTarget creation to fail.

Specifying a Pixel Format for an ID2D1Bitmap

Generally, ID2D1Bitmap objects support the following formats and alpha modes (with some restrictions, described in the paragraphs that follow.)

FormatAlpha mode
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_STRAIGHT
DXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_UNKNOWND2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_B8G8R8X8_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_BC1_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_BC1_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_BC1_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_BC2_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_BC2_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_BC2_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_UNKNOWN
DXGI_FORMAT_BC3_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_PREMULTIPLIED
DXGI_FORMAT_BC3_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_IGNORE
DXGI_FORMAT_BC3_UNORM (Windows 8.1 and later, only) D2D1_ALPHA_MODE_UNKNOWN

 

When you use the ID2D1RenderTarget::CreateSharedBitmap method, you use the pixelFormat field of a D2D1_BITMAP_PROPERTIES structure to specify the pixel format of the new render target. It must match the pixel format of the ID2D1Bitmap source.

When you use the CreateBitmapFromWicBitmap method, you use the pixelFormat field of a D2D1_BITMAP_PROPERTIES structure (instead of the pixelFormat member of a D2D1_RENDER_TARGET_PROPERTIES structure) to specify the pixel format of the new render target. It must match the pixel format of the WIC bitmap source.

Note  For more information about support for block compressed (BCn) pixel formats, see Block compression.

Supported WIC Formats

When you use the CreateBitmapFromWicBitmap method to create a bitmap from a WIC bitmap, or when you use the CreateSharedBitmap method with an IWICBitmapLock, the WIC source must be in a format supported by Direct2D.

WIC formatCorresponding DXGI formatCorresponding alpha mode
GUID_WICPixelFormat8bppAlphaDXGI_FORMAT_A8_UNORMD2D1_ALPHA_MODE_STRAIGHT or D2D1_ALPHA_MODE_PREMULTIPLIED
GUID_WICPixelFormat32bppPRGBADXGI_FORMAT_R8G8B8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED or D2D1_ALPHA_MODE_IGNORE
GUID_WICPixelFormat32bppBGRDXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_IGNORE
GUID_WICPixelFormat32bppPBGRADXGI_FORMAT_B8G8R8A8_UNORMD2D1_ALPHA_MODE_PREMULTIPLIED

 

For an example that shows how to convert a WIC bitmap to a supported format, see How to Load a Bitmap from a File.

Using an Unsupported Format

Using any combination other than the pixel formats and alpha modes that are listed in the earlier tables results in a D2DERR_UNSUPPORTED_PIXEL_FORMAT or an E_INVALIDARG error.

About Alpha Modes

About Premultiplied and Straight Alpha Modes

The D2D1_ALPHA_MODE enumeration indicates whether the alpha channel uses premultiplied alpha, straight alpha, or should be ignored and considered opaque. With straight alpha, the alpha channel indicates a value that corresponds to how transparent a color is.

Colors are always treated as straight alpha by Direct2D drawing commands and brushes, regardless of the destination format.

With premultiplied alpha, each color channel is scaled by the alpha value. Typically, no color channel value is greater than the alpha channel value. If a color channel value in a pre-multiplied format is greater than the alpha channel, the standard source-over blending math creates an additive blend.

The value of the alpha channel itself is the same in both straight and pre-multiplied alpha.

The Differences Between Straight and Premultiplied Alpha

When describing an RGBA color by using straight alpha, the alpha value of the color is stored in the alpha channel. For example, to describe a red color that is 60% opaque, use the following values: (255, 0, 0, 255 * 0.6) = (255, 0, 0, 153). The 255 value indicates full red, and 153 (which is 60 percent of 255) indicates that the color should have an opacity of 60 percent.

When describing an RGBA color by using premultiplied alpha, each color is multiplied by the alpha value: (255 * 0.6, 0 * 0.6, 0 * 0.6, 255 * 0.6) = (153, 0, 0, 153).

Regardless of the alpha mode of the render target, D2D1_COLOR_F values are always interpreted as straight alpha. For example, when specifying the color of an ID2D1SolidColorBrush for use with a render target that uses the premultiplied alpha mode, specify the color just as you would if the render target used straight alpha. When you paint with the brush, Direct2D translates the color to the destination format for you.

Alpha Mode for Render Targets

Regardless of the alpha mode setting, a render target's contents support transparency. For example, if you draw a partly transparent red rectangle with a render target with an alpha mode of D2D1_ALPHA_MODE_IGNORE, the rectangle will appear pink (if the background is white).

If you draw a partly transparent red rectangle when the alpha mode is D2D1_ALPHA_MODE_PREMULTIPLIED, the rectangle will appear pink (assuming the background is white) and you can see through it to whatever is behind the render target. This is useful when you use a ID2D1DCRenderTarget to render to a transparent window or when you use an compatible render target (a render targeted created by the CreateCompatibleRenderTarget method) to create a bitmap that supports transparency.

ClearType and Alpha Modes

If you specify an alpha mode other than D2D1_ALPHA_MODE_IGNORE for a render target, the text antialiasing mode automatically changes from D2D1_TEXT_ANTIALIAS_MODE CLEARTYPE to D2D1_TEXT_ANTIALIAS_MODE GRAYSCALE. (When you specify an alpha mode of D2D1_ALPHA_MODE_UNKNOWN, Direct2D sets the alpha for you, depending on the kind of render target.)

You can use the SetTextAntialiasMode method to change the text antialias mode back to D2D1_TEXT_ANTIALIAS_MODE CLEARTYPE, but rendering ClearType text to a transparent surface can create unpredictable results. If you want to render ClearType text to an transparent render target, we recommend that you use one of the following two techniques.

  • Use the PushAxisAlignedClip method to clip the render target to the area where the text will be rendered, then call the Clear method and specify an opaque color, then render your text.
  • Use DrawRectangle to draw an opaque rectangle behind the area where the text will be rendered.

Related topics

D2D1_PIXEL_FORMAT
D2D1_ALPHA_MODE
DXGI_FORMAT