Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Refer to the title!!!
I have tried several kinds of way to achieve that, but the efficience is pretty low!

Bitmap bmpGlow(szeFont.Width, szeFont.Height);
Graphics gGlow(&bmpGlow);
gGlow.SetSmoothingMode(SmoothingModeAntiAlias);
gGlow.SetTextRenderingHint(TextRenderingHintAntiAlias);
gGlow.SetInterpolationMode(InterpolationModeHighQualityBicubic);
float fAMin = (float)m_nGlowAlphaMin;
float fAMax = (float)m_nGlowAlphaMax;
if(m_nLWidth < 8)
{
for(int nCell=1; nCell<= m_nLWidth; nCell++)
{
Color clrGlow;
MakeColor(clrGlow, BYTE(fAMax- (fAMax- fAMin)/m_nLWidth * nCell), m_clrGlow.GetR(), m_clrGlow.GetG(), m_clrGlow.GetB());
Pen penGlow(clrGlow, nCell*WIDTH_RATE);
penGlow.SetLineJoin(LineJoinRound);
gGlow.DrawPath(&penGlow, &path);
}
}
else
{
float fCellSeed = m_nLWidth/8.0f;
for(float fCell=fCellSeed; fCell<=m_nLWidth; fCell+=fCellSeed)
{
Color clrGlow;
MakeColor(clrGlow, BYTE(fAMax- (fAMax- fAMin)/m_nLWidth * fCell), m_clrGlow.GetR(), m_clrGlow.GetG(), m_clrGlow.GetB());
Pen penGlow(clrGlow, fCell*WIDTH_RATE);
penGlow.SetLineJoin(LineJoinRound);
gGlow.DrawPath(&penGlow, &path);
}
}
CachedBitmap cacheBitmap(&bmpGlow, bmpg);
bmpg->DrawCachedBitmap(&cacheBitmap, 0, 0);
Posted
Updated 30-Nov-09 14:50pm
v2

You can't draw it as you need it, you need to store it and show it. GDI+ is not efficient for real time stuff.
 
Share this answer
 
I do store it and then show it,But as the width of the glow increase it will take me too much time to achieve that!!!Can you show me something more detailed!
 
Share this answer
 
How slow does it render and how fast do you want it to render?
 
Share this answer
 
A 10 pixel width glow effect will take me about 200ms!!!How can I improve that!
 
Share this answer
 
You could always make a pixel shader to perform the glow effect on the GPU, but GDI+ doesn't support pixel shaders as far as I know. DirectX supports pixel shaders though.
 
Share this answer
 
I will make a string path and then call drawpath several times! The function of drawpath takes too much time to excute itself!I have tried other ways,but they just don't make sense!!
 
Share this answer
 
Well, if you want an algorithmic solution, you could try something I call a "voronoi gradient". Basically, you start with a set of pixels (in this case, they would be the rendered text). You then spawn "bots" from each of those pixels (and those bots remember the pixel they started on as their "source"). These bots each look at their 8 neighbor pixels and spawn bots at those pixels if the distance between that pixel and their source is lesser than that of any bot that previously visited that pixel. The parent bot then "dies" (i.e., is excluded from further processing) and the child bots repeat the same step in the next iteration of the algorithm. These bots do this spreading and spawning until they reach some distance from their source (say, 5 pixels). You then paint each pixel according to the value left behind by each bot (which would be the distance that pixel is from the closest source pixel). If you make closer pixels one color and farther pixels another color (i.e., a gradient), you produce a glow effect. You can even decide on the drop off you want to use in the gradient (i.e., a linear drop off, or a more rounded circular drop off). I got this algorithm working at a speed that allowed for smooth game play, but it was slower than it should be, probably due to poor memory management (this could be solved by using an object pool for the bots). I think this solution could work very well for you.

There is even an optimization you can use to speed things up. You can render the widest glow you plan on encountering. Then, save pixels according to their distance from the source. So, pixels at the source will be saved in MyPixels[0]. And pixels < 1 pixel away will be saved in MyPixels[1]. And pixels < 2 pixels (but > 1 pixel) away will be saved in MyPixels[2]. And so on. Then, if you want to render a 5 pixel glow, you just draw all the pixels in MyPixels[0], MyPixels[1], MyPixels[2], MyPixels[3], MyPixels[4], and MyPixels[5]. The MyPixels variable would be defined like this (C#):
C#
List<List<Pixel2DWithColorIntensity>> MyPixels;

Come to think of it, you could use this same technique without implementing the whole voronoi gradient algorithm. You could just use GDI to render your text with the largest glow you expect, then store pixels into MyPixels according to the intensity of their color. Either way, you have options. And the result will be VERY fast (just need to draw a list of pixels).
 
Share this answer
 
v2
Oh, and to add to my previous answer, there is a faster and more flexible way of doing things.

Render each character you plan on using (say, a-z, 0-9, plus puncuation) using the largest glow you expect to use. Split up colors into arrays based on their intensity (using the method I described in my previous answer). Then store the lists of pixels for each of those characters in a dictionary with a string key and a list value (actually, a list of lists of pixels). When you have to render a string, you visit each character in the string and get that character's pixels from the dictionary. You then draw each pixel up to a given glow radius. When a pixel is drawn this is more intense than the previous pixel drawn, that means it was closer to the source and so should overwrite the existing pixel (otherwise, leave the existing pixel). Naturally, you will have to transform the color intensity found in the dictionary to one suitable for the glow radius you are using. Keep in mind that this will cause you to lose some fancy text rendering features, such as kerning, but you can re-implement those if you want. Here is how the dictionary variable would be defined (C#):
C#
Dictionary<string, List<List<Pixel2DWithColorIntensity>>> RenderedCharacters;

Then, when you want to render the letter "e" (for example), you'd get the points by saying:
C#>var

 
Share this answer
 
Okay! I get to think over about that!Thanks, you make get a new idea!A solution!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900