/// <summary> /// Fills <see cref="Rectangle"/> with Colors that smoothly fades from one side to the other. /// </summary> /// <param name="rectangle"><see cref="Rectangle"/> to fill</param> /// <param name="startColor">Start Color for the fill</param> /// <param name="endColor">End Color for the fill</param> /// <param name="direction">Direction of the fade from Start to End</param> /// <remarks> /// This method only supports linear gradient fills /// </remarks> /// <exception cref="PlatformNotSupportedException"> /// Thrown when this method is used in a device older than Pocket PC 2003 /// </exception> public void GradientFill(Rectangle rectangle, Color startColor, Color endColor, GradientFillDirection direction) { var tva = new TRIVERTEX[2]; tva[0] = new TRIVERTEX(rectangle.Right, rectangle.Bottom, endColor); tva[1] = new TRIVERTEX(rectangle.X, rectangle.Y, startColor); var gra = new[] { new GRADIENT_RECT(0, 1) }; var hdc = Surface.GetHdc(); try { if (Environment.OSVersion.Platform == PlatformID.WinCE) { NativeMethods.GradientFill( hdc, tva, (uint)tva.Length, gra, (uint)gra.Length, (uint)direction); } else { NativeMethods.GdiGradientFill( hdc, tva, (uint)tva.Length, gra, (uint)gra.Length, (uint)direction); } } catch (MissingMethodException) { throw new PlatformNotSupportedException("Gradient Fill is not supported in this platform"); } finally { Surface.ReleaseHdc(hdc); } }
public static void GradientFill(this Graphics g, Rectangle rec, Color fromColor, Color toColor, GradientFillDirection direction, Blend blend = null) { int angle = 0; switch (direction) { case GradientFillDirection.Horizontal: angle = 0; break; case GradientFillDirection.Vertical: angle = 90; break; } g.GradientFillAtAngle(rec, fromColor, toColor, angle, blend); }