Csharp/C Sharp/2D Graphics/Path
Содержание
- 1 Add a rectangle to a Path
- 2 Add Bezier to a Path
- 3 Add closed figures to GraphicsPath
- 4 Add Ellipse to path
- 5 Add Line and Arc to Path
- 6 Add lines to Path
- 7 Close all figures
- 8 Create Path from String
- 9 Draw path
- 10 Fill path and draw path
- 11 Fillpath Demo
- 12 GraphicsPath closes a figure and starts a new figure
- 13 Graphics Path Example
- 14 GraphicsPath starts a new figure
- 15 GraphicsPath starts two figures and close them
- 16 Is click inside a path
- 17 Scribble with Path
- 18 Start A Figure
- 19 Widen a Path
Add a rectangle to a Path
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; 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; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddRectangle(new Rectangle(10, 50, 80, 20)); gp.AddEllipse(50, 10, 20, 80); g.DrawPath(Pens.Black, gp); gp.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Add Bezier to a Path
<source lang="csharp">
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); }
}
</source>
Add closed figures to GraphicsPath
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); // Create a figure gp.AddRectangle(new Rectangle(10, 50, 80, 20)); // Create another figure gp.AddEllipse(50, 10, 20, 80); g.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Add Ellipse to path
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class Clover: Form {
public static void Main() { Application.Run(new Clover()); } public Clover() { 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) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, cy / 3, cx / 2, cy / 3); // Left grfx.SetClip(path); grfx.TranslateTransform(cx / 2, cy / 2); Pen pen = new Pen(clr); float fRadius = (float) Math.Sqrt(Math.Pow(cx / 2, 2) + Math.Pow(cy / 2, 2)); for (float fAngle = 0; fAngle < (float) Math.PI * 2; fAngle += (float) Math.PI / 180) { grfx.DrawLine(pen, 0, 0, fRadius * (float) Math.Cos(fAngle), -fRadius * (float) Math.Sin(fAngle)); } }
}
</source>
Add Line and Arc to Path
<source lang="csharp"> 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); }
}
</source>
Add lines to Path
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = "Pen Cap App"; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.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.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }
</source>
Close all figures
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; 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; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70); gp.AddLine(30, 70, 60, 70); gp.StartFigure(); gp.AddLine(60, 110, 40, 160); gp.CloseAllFigures(); g.DrawPath(Pens.Black, gp); gp.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Create Path from String
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddString("www.nfex.ru", new FontFamily("Times New Roman"), (int)(FontStyle.Bold | FontStyle.Italic), 144, new Point(5, 5), StringFormat.GenericTypographic); g.SetClip(gp); g.DrawImage(new Bitmap("winter.jpg"), this.ClientRectangle); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Draw path
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.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.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } static void Main() { Application.Run(new Form1()); }
}
</source>
Fill path and draw path
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); // Create an open figure gp.AddLine(10, 10, 10, 50); gp.AddLine(10, 50, 50, 50); // Start a new figure gp.StartFigure(); gp.AddLine(60, 10, 60, 50); gp.AddLine(60, 50, 100, 50); gp.AddLine(100, 50, 100, 10); gp.CloseFigure(); gp.AddEllipse(new Rectangle(110, 10, 40, 40)); g.FillPath(Brushes.Orange, gp); g.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }
</source>
Fillpath Demo
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; 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 }
}
</source>
GraphicsPath closes a figure and starts a new figure
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); // Create a figure gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70); gp.AddLine(30, 70, 60, 70); gp.CloseFigure(); gp.StartFigure(); gp.AddLine(60, 110, 40, 160); g.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Graphics Path Example
<source lang="csharp"> /* 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.ruponentModel; 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.ruponentModel.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."); } } }
}
</source>
GraphicsPath starts a new figure
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); // Create a figure gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 130, 55, 25, 70, 30, 70); gp.AddLine(30, 70, 60, 70); gp.AddBezier(60, 70, 555, 70, 90, 55, 90, 50); gp.AddLine(90, 50, 90, 30); // Create another figure gp.StartFigure(); gp.AddLine(60, 110, 40, 160); gp.AddLine(40, 160, 60, 180); g.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
GraphicsPath starts two figures and close them
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); // Create a figure gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 100, 55, 25, 70, 30, 70); gp.AddLine(30, 70, 60, 70); // Create another figure gp.StartFigure(); gp.AddLine(60, 110, 40, 160); gp.AddLine(40, 160, 60, 180); // Close all figures gp.CloseAllFigures(); // Draw the path g.DrawPath(Pens.Black, gp); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }
</source>
Is click inside a path
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; public class Form1 : Form {
GraphicsPath path; public Form1() { InitializeComponent(); } private void GraphicsPathHit_Paint(object sender, 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 GraphicsPathHit_MouseDown(object sender, MouseEventArgs e) { if (path.IsVisible(e.X, e.Y)) { MessageBox.Show("You clicked inside the figure."); } } private void InitializeComponent() { this.SuspendLayout(); // // GraphicsPathHit // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "GraphicsPathHit"; this.Text = "GraphicsPathHit"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.GraphicsPathHit_Paint); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicsPathHit_MouseDown); this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>
Scribble with Path
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class ScribbleWithPath: Form {
GraphicsPath path= new GraphicsPath(); bool bTracking; Point ptLast; public static void Main() { Application.Run(new ScribbleWithPath()); } protected override void OnMouseDown(MouseEventArgs mea) { if (mea.Button != MouseButtons.Left) return; ptLast = new Point(mea.X, mea.Y); bTracking = true; path.StartFigure(); } protected override void OnMouseMove(MouseEventArgs mea) { if (!bTracking) return; Point ptNew = new Point(mea.X, mea.Y); Graphics grfx = CreateGraphics(); grfx.DrawLine(new Pen(ForeColor), ptLast, ptNew); grfx.Dispose(); path.AddLine(ptLast, ptNew); ptLast = ptNew; } protected override void OnMouseUp(MouseEventArgs mea) { bTracking = false; } protected override void OnPaint(PaintEventArgs pea) { pea.Graphics.DrawPath(new Pen(ForeColor), path); }
}
</source>
Start A Figure
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; 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; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70); gp.StartFigure(); gp.AddLine(60, 110, 40, 160); g.DrawPath(Pens.Black, gp); gp.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Widen a Path
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class WidenPath: Form {
GraphicsPath path= new GraphicsPath(); public static void Main() { Application.Run(new WidenPath()); } public WidenPath() { ResizeRedraw = true; path.AddLines(new Point[] { new Point(20, 10), new Point(50, 50), new Point(80, 10) }); path.AddPolygon(new Point[] { new Point(20, 30), new Point(50, 70), new Point(80, 30) }); } 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) { grfx.ScaleTransform(cx / 300f, cy / 200f); for (int i = 0; i < 6; i++) { GraphicsPath pathClone = (GraphicsPath) path.Clone(); Matrix matrix = new Matrix(); Pen penThin = new Pen(clr, 1); Pen penThick = new Pen(clr, 5); Pen penWiden = new Pen(clr, 7.5f); Brush brush = new SolidBrush(clr); matrix.Translate((i % 3) * 100, (i / 3) * 100); if (i < 3) pathClone.Transform(matrix); else pathClone.Widen(penWiden, matrix); switch (i % 3) { case 0: grfx.DrawPath(penThin, pathClone); break; case 1: grfx.DrawPath(penThick, pathClone); break; case 2: grfx.FillPath(brush, pathClone); break; } } }
}
</source>