Wednesday, 29 January 2014

Graphics Path to draw the Elements in WinForms

In this article we are going to see how to draw a elements in the Winforms, To draw the elements we have graphics objects in the paint event. To do the Draw we are using Graphics Path, where we are plotting the points to draw and fill the color. In Graphics path we are adding many elements like line, Ellipse, Curve and Bezier. There is method called Start Figure which will use to draw a figure by the all elements points to finish the figure we have to draw and connect the points by called the CloseFigure(). Finally give the path to the Graphics to draw.

C#:
    public class AnimatedForm:Form
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            path.AddLine(20, 50, 70, 230);
            path.AddArc(30, 60, 70, 140, 130, 120);
            path.AddBezier(30, 40, 50, 20, 50, 60, 100, 20);
            path.CloseFigure();

            path.AddEllipse(60, 50, 180, 180);
           
            PathGradientBrush pgb = new PathGradientBrush(path);
            pgb.SurroundColors = new Color[] { Color.Green,Color.Yellow,Color.Red,
                                            Color.Blue,Color.Orange, Color.White,
                                          };
            e.Graphics.FillPath(pgb, path);
            e.Graphics.DrawPath(Pens.Black, path);  
            path.Dispose();         
        }

    }   
    public partial class Form1 :AnimatedForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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()
        {
            this.SuspendLayout();
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(288, 262);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

    }


Output:


From this post you can learn how to implement the draw in the forms.

1 comment:

  1. This programming has the .Net Developer using Elements of Winforms. It's for the graphical analysis with programming.Also Useful for my carrier.

    Dotnet Training in Chennai

    ReplyDelete