using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class SquareTile: Form { const int iSide = 50; // Side of square public static void Main() { Application.Run(new SquareTile()); } public SquareTile() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Point[] apt = {new Point(0, 0), new Point(iSide, 0), new Point(iSide, iSide), new Point(0, iSide)}; PathGradientBrush pgbrush = new PathGradientBrush(apt, WrapMode.TileFlipXY); pgbrush.SurroundColors = new Color[] { Color.Red, Color.Lime, Color.Blue, Color.White}; grfx.FillRectangle(pgbrush, 0, 0, cx, cy); } }
2D Graphics
Triangle Tile
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class TriangleTile: Form { const int iSide = 50; // Side of square for triangle MenuItem miChecked; public static void Main() { Application.Run(new TriangleTile()); } public TriangleTile() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&Wrap-Mode"); foreach (WrapMode wm in Enum.GetValues(typeof(WrapMode))) { MenuItem mi = new MenuItem(); mi.Text = wm.ToString(); mi.Click += new EventHandler(MenuWrapModeOnClick); Menu.MenuItems[0].MenuItems.Add(mi); } miChecked = Menu.MenuItems[0].MenuItems[0]; miChecked.Checked = true; } void MenuWrapModeOnClick(object obj, EventArgs ea) { miChecked.Checked = false; miChecked = (MenuItem) obj; miChecked.Checked = true; Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Point[] apt = { new Point(0, 0), new Point(iSide, 0), new Point(0, iSide)}; PathGradientBrush pgbrush = new PathGradientBrush(apt, (WrapMode) miChecked.Index); grfx.FillRectangle(pgbrush, 0, 0, cx, cy); } }
Gradient Brush Ball
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class BouncingGradientBrushBall : Form { public static void Main() { Application.Run(new BouncingGradientBrushBall()); } public BouncingGradientBrushBall() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { GraphicsPath path = new GraphicsPath(); Rectangle rect = new Rectangle(10, 10, 30, 30); path.AddEllipse(rect); PathGradientBrush pgbrush = new PathGradientBrush(path); pgbrush.CenterPoint = new PointF((rect.Left + rect.Right) / 3, (rect.Top + rect.Bottom) / 3); pgbrush.CenterColor = Color.White; pgbrush.SurroundColors = new Color[] { Color.Red }; pea.Graphics.FillRectangle(pgbrush, rect); } }
Star Gradient Brush
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class StarGradientBrush: Form
{
public static void Main()
{
Application.Run(new StarGradientBrush());
}
public StarGradientBrush()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Point[] apt = new Point[5];
for (int i = 0; i < apt.Length; i++) { double dAngle = (i * 0.8 - 0.5) * Math.PI; apt[i] = new Point( (int)(cx * (0.50 + 0.48 * Math.Cos(dAngle))), (int)(cy * (0.50 + 0.48 * Math.Sin(dAngle)))); } PathGradientBrush pgbrush = new PathGradientBrush(apt); pgbrush.CenterColor = Color.White; pgbrush.SurroundColors = new Color[1] { Color.Black }; grfx.FillRectangle(pgbrush, 0, 0, cx, cy); } } [/csharp]
Triangle Gradient Brush
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class TriangleGradientBrush: Form { public static void Main() { Application.Run(new TriangleGradientBrush()); } public TriangleGradientBrush() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Point[] apt = { new Point(cx, 0), new Point(cx, cy), new Point(0, cy) }; PathGradientBrush pgbrush = new PathGradientBrush(apt); grfx.FillRectangle(pgbrush, 0, 0, cx, cy); } }
CenterColor
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; GraphicsPath gp = new GraphicsPath(); gp.AddLine(10, 10, 110, 15); gp.AddLine(110, 15, 100, 96); gp.AddLine(100, 96, 15, 110); gp.CloseFigure(); g.FillRectangle(Brushes.White, this.ClientRectangle); g.SmoothingMode = SmoothingMode.AntiAlias; PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.White; pgb.SurroundColors = new Color[] { Color.Blue }; g.FillPath(pgb, gp); g.DrawPath(Pens.Black, gp); pgb.Dispose(); gp.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
Fillpath Demo
using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class TestGDI2 : System.Windows.Forms.Form{ //in order to paint something OnPaint method needs to be overridden protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) { //OnPaint method is a member of Form class //The following call sends pe to an event listener Graphics base.OnPaint(pe); Graphics g = pe.Graphics; g.FillRectangle(new SolidBrush(Color.White), ClientRectangle); GraphicsPath path = new GraphicsPath(new Point[] { new Point(40, 140), new Point(275, 200), new Point(105, 225), new Point(190, 300), new Point(50, 350), new Point(20, 180), }, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Line, (byte)PathPointType.Line, }); PathGradientBrush pgb = new PathGradientBrush(path); pgb.SurroundColors = new Color[] { Color.Green,Color.Yellow,Color.Red, Color.Blue,Color.Orange, Color.White, }; g.FillPath(pgb, path); } public static void Main() { System.Windows.Forms.Application.Run(new TestGDI2());//display form } }