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 } }
Draw path
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form{ private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } protected override void OnPaint (PaintEventArgs e) { GraphicsPath path; path = new GraphicsPath(new Point[]{ new Point(10, 10), new Point(50, 10), new Point(20, 150), new Point(10, 50), new Point(200, 160)}, new byte[] {(byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line }); e.Graphics.DrawPath(Pens.Black, path); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } static void Main() { Application.Run(new Form1()); } }
Graphics Path Example
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace GDI_Basics { /// <summary> /// Summary description for GraphicsPathExample. /// </summary> public class GraphicsPathExample : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public GraphicsPathExample() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { // // GraphicsPathExample // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "GraphicsPathExample"; this.Text = "GraphicsPathExample"; this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicsPathExample_MouseDown); this.Paint += new System.Windows.Forms.PaintEventHandler(this.GraphicsPathExample_Paint); } #endregion [STAThread] static void Main() { Application.Run(new GraphicsPathExample()); } GraphicsPath path; private void GraphicsPathExample_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; path = new GraphicsPath(); path.StartFigure(); path.AddArc(10, 10, 100, 100, 20, 50); path.AddLine(20, 50, 70, 230); path.CloseFigure(); path.AddEllipse(120, 50, 80, 80); e.Graphics.FillPath(Brushes.White, path); e.Graphics.DrawPath(Pens.Black, path); } private void GraphicsPathExample_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (path.IsVisible(e.X, e.Y)) { MessageBox.Show("You clicked inside the figure."); } } } }
Add Line and Arc to Path
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class LineArcPath: Form { public static void Main() { Application.Run(new LineArcPath()); } 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) { GraphicsPath path = new GraphicsPath(); Pen pen = new Pen(clr, 25); path.AddLine( 25, 100, 125, 100); path.AddArc (125, 50, 100, 100, -180, 180); path.AddLine(225, 100, 325, 100); grfx.DrawPath(pen, path); } }
Add Bezier to a Path
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class Flower: Form
{
public static void Main()
{
Application.Run(new Flower());
}
public Flower()
{
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)
{
float fScale = Math.Min(cx, cy) / 200f;
grfx.TranslateTransform(cx / 2, cy / 2);
grfx.ScaleTransform(fScale, fScale);
GraphicsPath path = new GraphicsPath();
path.AddBezier(new Point( 0, 0), new Point(125, 125),
new Point(75, 15), new Point(600, 0));
for (int i = 0; i < 8; i++) { grfx.FillPath(Brushes.Red, path); grfx.DrawPath(Pens.Black, path); grfx.RotateTransform(360 / 8); } Rectangle rect = new Rectangle(-150, -150, 300, 300); grfx.FillEllipse(Brushes.Yellow, rect); grfx.DrawEllipse(Pens.Black, rect); } } [/csharp]