【问题标题】:How to draw a lightened border(outer glow effect)?如何绘制亮化边框(外发光效果)?
【发布时间】:2012-07-26 03:44:59
【问题描述】:

如何使用 gdi/gdi+ 绘制像这样的亮边框:

谁能给我一个思路?谢谢。

【问题讨论】:

  • 这需要一个 LinearGradientBrush。或 DrawImage()。

标签: c++ algorithm graphics gdi+ gdi


【解决方案1】:

使用 GDI+,我建议您使用 PathGradientBrush。它允许您用边缘周围的一系列颜色填充区域,这些颜色都向中心颜色混合。在这种情况下,您可能只需要 1 种边缘颜色。为圆角矩形创建一个 GraphicsPath 并使用 FillPath() 用 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);

【讨论】:

  • 类似,但不是我想要的效果......而且我无法准确控制发光......也谢谢你
【解决方案2】:
  • 将边框绘制成比边框本身稍大的图像。
  • 模糊它。
  • 擦除边框内部。
  • 在模糊图像上绘制边框。
  • 将该图像绘制到目的地。

【讨论】:

    最近更新 更多