5

How can i draw a lightened border like this with gdi/gdi+: enter image description here

Anyone can give me a train of thought?Thanks.

toki
  • 411
  • 4
  • 14

2 Answers2

2

Using GDI+, I would recommend you use a PathGradientBrush. It allows you to fill a region with series of colors around the edge that all blend toward a center color. You probably only need 1 edge color in this case. Create a GraphicsPath for a rounded rectangle and use FillPath() to fill it with a PathGradientBrush:

GraphicsPath graphicsPath;

//rect - for a bounding rect
//radius - for how 'rounded' the glow will look
int diameter = radius * 2;

graphicsPath.AddArc(Rect(rect.X, rect.Y, diameter, diameter) 180.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X + rect.Width - diameter, rect.Y, diameter, diameter), 270.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X + rect.Width - diameter, rect.Y + rect.Height - diameter, diameter, diameter), 0.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X, rect.Y + rect.Height - diameter, diameter, diameter), 90.0f, 90.0f);
graphicsPath.CloseFigure();

PathGradientBrush brush(&graphicsPath);
brush.SetCenterColor(centerColor); //would be some shade of blue, following your example
int colCount = 1;
brush.SetSurroundColors(surroundColor, &colCount); //same as your center color, but with the alpha channel set to 0

//play with these numbers to get the glow effect you want
REAL blendFactors[] = {0.0, 0.1, 0.3, 1.0};
REAL blendPos[] = {0.0, 0.4, 0.6, 1.0};
//sets how transition toward the center is shaped
brush.SetBlend(blendFactors, blendPos, 4);
//sets the scaling on the center. you may want to have it elongated in the x-direction
brush.SetFocusScales(0.2f, 0.2f);

graphics.FillPath(&brush, &graphicsPath);
Ryand
  • 436
  • 4
  • 16
  • similar, but not quite the same effect i want...and I can not control the glow accuatly......thank you as well – toki Jul 31 '12 at 12:06
1
  • Draw the border into an image slightly larger than the border itself.
  • Blur it.
  • Erase the inside of the border.
  • Draw the border over the blurred image.
  • Draw that image to the destination.
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622