PathGradientBrush
Encapsulates a Brush object that fills the interior of a GraphicsPath object with a gradient. This class cannot be inherited.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.Win32;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.Graphics.GdiPlus;
namespace System.Drawing.Drawing2D
{
[NullableContext(1)]
[Nullable(0)]
public sealed class PathGradientBrush : Brush
{
[Nullable(0)]
internal unsafe GpPathGradient* NativePathGradient {
[NullableContext(0)]
get {
return (GpPathGradient*)base.NativeBrush;
}
}
public unsafe Color CenterColor {
get {
ARGB argb = default(ARGB);
PInvoke.GdipGetPathGradientCenterColor(NativePathGradient, (uint*)(&argb)).ThrowIfFailed();
GC.KeepAlive(this);
return argb;
}
set {
PInvoke.GdipSetPathGradientCenterColor(NativePathGradient, (ARGB)ref value).ThrowIfFailed();
GC.KeepAlive(this);
}
}
public unsafe Color[] SurroundColors {
get {
int length = default(int);
PInvoke.GdipGetPathGradientSurroundColorCount(NativePathGradient, &length).ThrowIfFailed();
ArgbBuffer argbBuffer = new ArgbBuffer(length);
try {
fixed (uint* color = &argbBuffer.GetPinnableReference()) {
PInvoke.GdipGetPathGradientSurroundColorsWithCount(NativePathGradient, color, &length).ThrowIfFailed();
}
GC.KeepAlive(this);
return argbBuffer.ToColorArray(length);
} finally {
argbBuffer.Dispose();
}
}
set {
ArgumentNullException.ThrowIfNull(value, "value");
ArgbBuffer argbBuffer = new ArgbBuffer(value);
try {
int num = value.Length;
fixed (uint* color = &argbBuffer.GetPinnableReference()) {
PInvoke.GdipSetPathGradientSurroundColorsWithCount(NativePathGradient, color, &num).ThrowIfFailed();
GC.KeepAlive(this);
}
} finally {
argbBuffer.Dispose();
}
}
}
public unsafe PointF CenterPoint {
get {
PointF result = default(PointF);
PInvoke.GdipGetPathGradientCenterPoint(NativePathGradient, (global::Windows.Win32.Graphics.GdiPlus.PointF*)(&result)).ThrowIfFailed();
GC.KeepAlive(this);
return result;
}
set {
PInvoke.GdipSetPathGradientCenterPoint(NativePathGradient, (global::Windows.Win32.Graphics.GdiPlus.PointF*)(&value)).ThrowIfFailed();
GC.KeepAlive(this);
}
}
public unsafe RectangleF Rectangle {
get {
RectangleF result = default(RectangleF);
PInvoke.GdipGetPathGradientRect(NativePathGradient, (RectF*)(&result)).ThrowIfFailed();
GC.KeepAlive(this);
return result;
}
}
public unsafe Blend Blend {
get {
int num = default(int);
PInvoke.GdipGetPathGradientBlendCount(NativePathGradient, &num).ThrowIfFailed();
float[] array = new float[num];
float[] array2 = new float[num];
fixed (float* blend = array) {
fixed (float[] array3 = array2) {
float* positions = (float*);
PInvoke.GdipGetPathGradientBlend(NativePathGradient, blend, positions, num).ThrowIfFailed();
}
}
array3 = null;
Blend result = new Blend(array.Length) {
Factors = array,
Positions = array2
};
GC.KeepAlive(this);
return result;
}
set {
ArgumentNullException.ThrowIfNull(value, "value");
ArgumentNullException.ThrowIfNull(value.Factors, "value.Factors");
if (value.Positions == null || value.Positions.Length != value.Factors.Length)
throw new ArgumentException(System.SR.Format(System.SR.InvalidArgumentValue, "value.Positions", value.Positions), "value");
int count = value.Factors.Length;
fixed (float* blend = value.Factors) {
fixed (float[] array = value.Positions) {
float* positions = (float*);
PInvoke.GdipSetPathGradientBlend(NativePathGradient, blend, positions, count).ThrowIfFailed();
GC.KeepAlive(this);
}
}
array = null;
}
}
public unsafe ColorBlend InterpolationColors {
get {
int num = default(int);
PInvoke.GdipGetPathGradientPresetBlendCount(NativePathGradient, &num).ThrowIfFailed();
if (num != 0) {
ArgbBuffer argbBuffer = new ArgbBuffer(num);
try {
float[] array = new float[num];
ColorBlend colorBlend = new ColorBlend(num);
fixed (uint* blend = &argbBuffer.GetPinnableReference()) {
fixed (float* positions = array) {
PInvoke.GdipGetPathGradientPresetBlend(NativePathGradient, blend, positions, num).ThrowIfFailed();
}
}
colorBlend.Positions = array;
colorBlend.Colors = argbBuffer.ToColorArray(num);
GC.KeepAlive(this);
return colorBlend;
} finally {
argbBuffer.Dispose();
}
}
return new ColorBlend();
}
set {
ArgumentNullException.ThrowIfNull(value, "value");
int count = value.Colors.Length;
if (value.Positions == null || value.Colors.Length != value.Positions.Length)
throw new ArgumentException(System.SR.Format(System.SR.InvalidArgumentValue, "value.Positions", value.Positions), "value");
float[] positions = value.Positions;
ArgbBuffer argbBuffer = new ArgbBuffer(value.Colors);
try {
fixed (float* positions2 = positions) {
fixed (uint* blend = &argbBuffer.GetPinnableReference()) {
PInvoke.GdipSetPathGradientPresetBlend(NativePathGradient, blend, positions2, count).ThrowIfFailed();
GC.KeepAlive(this);
}
}
} finally {
argbBuffer.Dispose();
}
}
}
public unsafe Matrix Transform {
get {
Matrix matrix = new Matrix();
PInvoke.GdipGetPathGradientTransform(NativePathGradient, matrix.NativeMatrix).ThrowIfFailed();
GC.KeepAlive(this);
return matrix;
}
set {
ArgumentNullException.ThrowIfNull(value, "value");
PInvoke.GdipSetPathGradientTransform(NativePathGradient, value.NativeMatrix).ThrowIfFailed();
GC.KeepAlive(value);
GC.KeepAlive(this);
}
}
public unsafe PointF FocusScales {
get {
float x = default(float);
float y = default(float);
PInvoke.GdipGetPathGradientFocusScales(NativePathGradient, &x, &y).ThrowIfFailed();
GC.KeepAlive(this);
return new PointF(x, y);
}
set {
PInvoke.GdipSetPathGradientFocusScales(NativePathGradient, value.X, value.Y).ThrowIfFailed();
GC.KeepAlive(this);
}
}
public unsafe WrapMode WrapMode {
get {
WrapMode result = default(WrapMode);
PInvoke.GdipGetPathGradientWrapMode(NativePathGradient, (global::Windows.Win32.Graphics.GdiPlus.WrapMode*)(&result)).ThrowIfFailed();
GC.KeepAlive(this);
return result;
}
set {
if ((value < WrapMode.Tile || value > WrapMode.Clamp) ? true : false)
throw new InvalidEnumArgumentException("value", (int)value, typeof(WrapMode));
PInvoke.GdipSetPathGradientWrapMode(NativePathGradient, (global::Windows.Win32.Graphics.GdiPlus.WrapMode)value).ThrowIfFailed();
GC.KeepAlive(this);
}
}
public PathGradientBrush(params PointF[] points)
: this(points, WrapMode.Clamp)
{
}
public PathGradientBrush(PointF[] points, WrapMode wrapMode)
: this(wrapMode, points.OrThrowIfNull("points").AsSpan())
{
}
[NullableContext(0)]
private unsafe PathGradientBrush(WrapMode wrapMode, [System.Runtime.CompilerServices.ParamCollection] [ScopedRef] ReadOnlySpan<PointF> points)
{
if ((wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) ? true : false)
throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
if (points.Length < 2)
throw new ArgumentException(null, "points");
fixed (PointF* points2 = &points.GetPinnableReference()) {
GpPathGradient* nativeBrushInternal = default(GpPathGradient*);
PInvoke.GdipCreatePathGradient((global::Windows.Win32.Graphics.GdiPlus.PointF*)points2, points.Length, (global::Windows.Win32.Graphics.GdiPlus.WrapMode)wrapMode, &nativeBrushInternal).ThrowIfFailed();
SetNativeBrushInternal((GpBrush*)nativeBrushInternal);
}
}
public PathGradientBrush(params Point[] points)
: this(points, WrapMode.Clamp)
{
}
[NullableContext(0)]
private PathGradientBrush([System.Runtime.CompilerServices.ParamCollection] [ScopedRef] ReadOnlySpan<Point> points)
: this(WrapMode.Clamp, points)
{
}
public PathGradientBrush(Point[] points, WrapMode wrapMode)
: this(wrapMode, points.OrThrowIfNull("points").AsSpan())
{
}
[NullableContext(0)]
private unsafe PathGradientBrush(WrapMode wrapMode, [System.Runtime.CompilerServices.ParamCollection] [ScopedRef] ReadOnlySpan<Point> points)
{
if ((wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) ? true : false)
throw new InvalidEnumArgumentException("wrapMode", (int)wrapMode, typeof(WrapMode));
if (points.Length < 2)
throw new ArgumentException(null, "points");
fixed (Point* points2 = &points.GetPinnableReference()) {
GpPathGradient* nativeBrushInternal = default(GpPathGradient*);
PInvoke.GdipCreatePathGradientI((global::Windows.Win32.Graphics.GdiPlus.Point*)points2, points.Length, (global::Windows.Win32.Graphics.GdiPlus.WrapMode)wrapMode, &nativeBrushInternal).ThrowIfFailed();
SetNativeBrushInternal((GpBrush*)nativeBrushInternal);
}
}
public unsafe PathGradientBrush(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
GpPathGradient* nativeBrushInternal = default(GpPathGradient*);
PInvoke.GdipCreatePathGradientFromPath(path._nativePath, &nativeBrushInternal).ThrowIfFailed();
SetNativeBrushInternal((GpBrush*)nativeBrushInternal);
}
[NullableContext(0)]
internal unsafe PathGradientBrush(GpPathGradient* nativeBrush)
{
SetNativeBrushInternal((GpBrush*)nativeBrush);
}
public unsafe override object Clone()
{
GpBrush* nativeBrush = default(GpBrush*);
PInvoke.GdipCloneBrush(base.NativeBrush, &nativeBrush).ThrowIfFailed();
GC.KeepAlive(this);
return new PathGradientBrush((GpPathGradient*)nativeBrush);
}
public void SetSigmaBellShape(float focus)
{
SetSigmaBellShape(focus, 1);
}
public unsafe void SetSigmaBellShape(float focus, float scale)
{
PInvoke.GdipSetPathGradientSigmaBlend(NativePathGradient, focus, scale).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetBlendTriangularShape(float focus)
{
SetBlendTriangularShape(focus, 1);
}
public unsafe void SetBlendTriangularShape(float focus, float scale)
{
PInvoke.GdipSetPathGradientLinearBlend(NativePathGradient, focus, scale).ThrowIfFailed();
GC.KeepAlive(this);
}
public unsafe void ResetTransform()
{
PInvoke.GdipResetPathGradientTransform(NativePathGradient).ThrowIfFailed();
GC.KeepAlive(this);
}
public void MultiplyTransform(Matrix matrix)
{
MultiplyTransform(matrix, MatrixOrder.Prepend);
}
public unsafe void MultiplyTransform(Matrix matrix, MatrixOrder order)
{
ArgumentNullException.ThrowIfNull(matrix, "matrix");
PInvoke.GdipMultiplyPathGradientTransform(NativePathGradient, matrix.NativeMatrix, (global::Windows.Win32.Graphics.GdiPlus.MatrixOrder)order).ThrowIfFailed();
GC.KeepAlive(matrix);
GC.KeepAlive(this);
}
public void TranslateTransform(float dx, float dy)
{
TranslateTransform(dx, dy, MatrixOrder.Prepend);
}
public unsafe void TranslateTransform(float dx, float dy, MatrixOrder order)
{
PInvoke.GdipTranslatePathGradientTransform(NativePathGradient, dx, dy, (global::Windows.Win32.Graphics.GdiPlus.MatrixOrder)order).ThrowIfFailed();
GC.KeepAlive(this);
}
public void ScaleTransform(float sx, float sy)
{
ScaleTransform(sx, sy, MatrixOrder.Prepend);
}
public unsafe void ScaleTransform(float sx, float sy, MatrixOrder order)
{
PInvoke.GdipScalePathGradientTransform(NativePathGradient, sx, sy, (global::Windows.Win32.Graphics.GdiPlus.MatrixOrder)order).ThrowIfFailed();
GC.KeepAlive(this);
}
public void RotateTransform(float angle)
{
RotateTransform(angle, MatrixOrder.Prepend);
}
public unsafe void RotateTransform(float angle, MatrixOrder order)
{
PInvoke.GdipRotatePathGradientTransform(NativePathGradient, angle, (global::Windows.Win32.Graphics.GdiPlus.MatrixOrder)order).ThrowIfFailed();
GC.KeepAlive(this);
}
}
}