using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Drawing2D; internal static class BrushExtensions { internal static Brush ToGdiBrush(this System.Windows.Media.Brush brush) { if (brush.GetType() == typeof(System.Windows.Media.SolidColorBrush)) { return new SolidBrush((brush as System.Windows.Media.SolidColorBrush).Color.ToGdiColor()); } else if (brush.GetType() == typeof(System.Windows.Media.LinearGradientBrush)) { System.Windows.Media.LinearGradientBrush b = brush as System.Windows.Media.LinearGradientBrush; double angle = Math.Atan2(b.EndPoint.Y - b.StartPoint.Y, b.EndPoint.X - b.StartPoint.X) * 180 / Math.PI; LinearGradientBrush gradient = new LinearGradientBrush(new Rectangle(0, 0, 200, 100), Color.Black, Color.Black, (float)angle); ColorBlend blend = new ColorBlend(); List colors = new List(); List offsets = new List(); foreach (var stop in b.GradientStops) { colors.Add(stop.Color.ToGdiColor()); offsets.Add((float)stop.Offset); } blend.Colors = colors.ToArray(); blend.Positions = offsets.ToArray(); gradient.InterpolationColors = blend; return gradient; } else { return new LinearGradientBrush(new PointF(0, 0), new Point(200, 100), Color.Black, Color.Black); } } }