ID2D1DCRenderTarget interface

Issues drawing commands to a GDI device context.


The ID2D1DCRenderTarget interface has these methods.

Method Description
ID2D1DCRenderTarget::BindDC Binds the render target to the device context to which it issues drawing commands.


Creating ID2D1DCRenderTarget Objects

To create an ID2D1DCRenderTarget, use the ID2D1Factory::CreateDCRenderTarget method.

Before you can render with the DC render target, you must use its BindDC method to associate it with a GDI DC. You do this each time you use a different DC, or the size of the area you want to draw to changes.

To enable the DC render target to work with GDI, set its pixel format to DXGI_FORMAT_B8G8R8A8_UNORM and its alpha mode to D2D1_ALPHA_MODE_PREMULTIPLIED or D2D1_ALPHA_MODE_IGNORE.

Your application should create render targets once and hold onto them for the life of the application or until the render target's EndDraw method returns the D2DERR_RECREATE_TARGET error. When you receive this error, you need to recreate the render target (and any resources it created).

ID2D1DCRenderTargets, GDI Transforms, and Right-to-Left Language Builds of Windows

When you use an ID2D1DCRenderTarget, it renders Direct2D content to an internal bitmap, and then renders the bitmap to the DC with GDI.

It's possible for GDI to apply a GDI transform (through the SetWorldTransform method) or other effect to the same DC used by the render target, in which case GDI transforms the bitmap produced by Direct2D. Using a GDI transform to transform the Direct2D content has the potential to degrade the visual quality of the output, because you're transforming a bitmap for which antialiasing and subpixel positioning have already been calculated.

For example, suppose you use the render target to draw a scene that contains antialiased geometries and text. If you use a GDI transform to apply a scale transform to the DC and scale the scene so that it's 10 times larger, you'll see pixelization and jagged edges. (If, however, you applied a similar transform using Direct2D, the visual quality of the scene would not be degraded.)

In some cases, it might not be obvious that GDI is performing additional processing that might degrade the quality of the Direct2D content. For example, on a right-to-left (RTL) build of Windows, content rendered by an ID2D1DCRenderTarget might be horizontally inverted when GDI copies it to its destination. Whether the content is actually inverted depends on the current settings of the DC.

Depending on the type of content being rendered, you might want to prevent the inversion. If the Direct2D content includes ClearType text, this inversion will degrade the quality of the text.

You can control RTL rendering behavior by using the SetLayout GDI function. To prevent the mirroring, call the SetLayout GDI function and specify LAYOUT_BITMAPORIENTATIONPRESERVEDas the only value for the second parameter (do not combine it with LAYOUT_RTL), as shown in the following example:



The following code creates a DC render target.

// Create a DC render target.
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(

hr = m_pD2DFactory->CreateDCRenderTarget(&props, &m_pDCRT);

In the preceding code, m_pD2DFactory is a pointer to an ID2D1Factory, and m_pDCRT is a pointer to an ID2D1DCRenderTarget.

The next code example binds a DC to the ID2D1DCRenderTarget.

HRESULT DemoApp::OnRender(const PAINTSTRUCT &ps)

// Get the dimensions of the client drawing area.
GetClientRect(m_hwnd, &rc);

// Bind the DC to the DC render target.
hr = m_pDCRT->BindDC(ps.hdc, &rc);

For more information about using GDI with Direct2D, see the Direct2D and GDI Interoperation Overview.


Minimum supported client Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2008 R2, Windows Server 2008 with SP2 and Platform Update for Windows Server 2008 [desktop apps | UWP apps]
Target Platform Windows
Header d2d1.h

See Also

Direct2D and GDI Interoperation Overview