MSDN Community

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;
};
PropertyTypeDescription
CenterColorColorGets or sets the color at the center of the gradient.
SurroundColorsColor[]Gets or sets the colors at the boundary of the path.
FocusScalesREAL[2]Specifies scaling factors that adjust the focus of the gradient.
GammaCorrectionBOOLEnables or disables gamma correction for the gradient.

Key Methods

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.