PathGradientBrush Class
Overview
Syntax
Properties
Methods
Example
The PathGradientBrush class defines a brush that fills the interior of a shape with a gradient that is defined by a set of colors and a path. It enables custom color blending and gamma correction for smooth color transitions.
Namespace
Gdiplus
Assembly
System.Drawing.dll
class PathGradientBrush : public Brush {
public:
PathGradientBrush(const Point* points, INT count);
PathGradientBrush(const PointF* points, INT count);
virtual ~PathGradientBrush();
void SetCenterColor(const Color& color);
Color GetCenterColor() const;
void SetSurroundColors(const Color* colors, INT count);
INT GetSurroundColorCount() const;
void GetSurroundColors(Color* colors, INT count) const;
void SetFocusScales(REAL xScale, REAL yScale);
void GetFocusScales(REAL* xScale, REAL* yScale) const;
void SetGammaCorrection(BOOL useGammaCorrection);
BOOL GetGammaCorrection() const;
};
| Property | Type | Description |
|---|---|---|
| CenterColor | Color | Gets or sets the color at the center of the gradient. |
| SurroundColors | Color[] | Gets or sets the colors at the boundary of the path. |
| FocusScales | REAL[2] | Specifies scaling factors that adjust the focus of the gradient. |
| GammaCorrection | BOOL | Enables or disables gamma correction for the gradient. |
Key Methods
SetCenterColor(const Color& color)– Assigns a color to the gradient’s center.GetCenterColor()– Retrieves the current center color.SetSurroundColors(const Color* colors, INT count)– Sets the colors that define the outer boundary.GetSurroundColors(Color* colors, INT count)– Obtains the current surround colors.SetFocusScales(REAL xScale, REAL yScale)– Adjusts the focus area of the gradient.GetFocusScales(REAL* xScale, REAL* yScale)– Retrieves the current focus scales.SetGammaCorrection(BOOL useGamma)– Enables/disables gamma correction.GetGammaCorrection()– Returns whether gamma correction is active.
Example – Drawing an Elliptical Gradient
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
void DrawGradient(HDC hdc) {
Graphics graphics(hdc);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
// Define the ellipse path
GraphicsPath path;
path.AddEllipse(100, 50, 200, 150);
// Create PathGradientBrush
PathGradientBrush pgb(&path);
pgb.SetCenterColor(Color(255, 255, 0, 0)); // Red center
Color surround[1] = { Color(255, 0, 0, 255) }; // Blue edge
pgb.SetSurroundColors(surround, 1);
pgb.SetGammaCorrection(TRUE);
// Fill the ellipse
graphics.FillPath(&pgb, &path);
}
// WinMain omitted for brevity
Run the program and you'll see an ellipse that blends from red at the center to blue at the border.