# Using Direct2D how would i fill a font with a gradient or radial brush?

This topic is 2884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive managed to color my text red but how do i color the text in using a color gradient or radial brush?

Here is part of the code where I guess it has to go..

static const WCHAR sc_helloWorld[] = L"Direct2D Template";

// Retrieve the size of the render target.
D2D1_SIZE_F renderTargetSize = m_pRenderTarget->GetSize();

m_pRenderTarget->BeginDraw();

// Sets background of window to black
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::Black));

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pRedBrush
);

hr = m_pRenderTarget->EndDraw();

The 2 brushes I would like to use are the following :

I want to use these brushes to create a color gradient fill within the text itself not in a rectangle that all the examples show.

Ive managed to create color gradient fills in rectangles using the following code..

// Translate for the linear gradient brush.

m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(200, 5)));

// Demonstrate a linear gradient brush.

But this obviously doesnt achieve what I want which is to fill the text in with a gradient fill.

##### Share on other sites
I've never done such kind of thing with Direct2D, but I guess it goes like this:

m_pRenderTarget->DrawText(sc_helloWorld,ARRAYSIZE(sc_helloWorld) - 1,m_pTextFormat,D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),m_pLinearGradientBrush //<--);

##### Share on other sites
Unfortunately ive already tried that as that was the obvious thing to do but nop thats not the answer as it doesnt fill the font with a gradient.

##### Share on other sites
Actually you were kinda right.. the way to do it was to use a color first then use a gradient fill like this..

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pRedBrush
);

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
);

And that seems to work.. COOL!

##### Share on other sites
You shouldn't need to draw the text twice. I took the time and tested it for you in my own application. So, using this:

//DrawTextLayout() is nearly the same as DrawText(), but it's faster when drawing//the same text multiple times. That what is important for you is the brush at the end of the function.D2DObjects->RenderTarget->DrawTextLayout(TextOrig,Layout,D2DObjects->BrushCollection.LinearReflectBrushHigh);`

I get this:

I hadn't got another gradient-brush handy, so I hope you can see that it's a gradient, and it works fine.

You really should find the issue on this, drawing text is not a very fast operation, and on top of that you don't use a layout. Can you show me the code you are using to create the gradient-brush?

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
11
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002302
×