Your Ads Here

Monday 27 February 2012

To draw a filled triangle or how to use brush on form using C#.NET


It shows how to fill triangle or how to use brush on form using C#.NET
Keywords: C#.net, CSharp.NET, triangle, drawing, graphics, fill, brush.

First import a namespace
using System.Drawing;

Place the code in the paint event for the form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testc
{
    public partial class Form1 : Form
    {
        Graphics gr_graphics = default(Graphics);
        //need a pen for drawing and make it black
        Pen pen_draw = new Pen(Color.Black);

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            gr_graphics = e.Graphics;
            //Draw a triangle on the form.
            //first have to define an array of points.
            Point[] pnt = new Point[3];
          
            pnt[0].X = 150;
            pnt[0].Y = 150;

            pnt[1].X = 150;
            pnt[1].Y = 200;

            pnt[2].X = 50;
            pnt[2].Y = 120;

            gr_graphics.DrawPolygon(pen_draw, pnt);

            //Do a filled triangle
            //First create a brush

                Brush brush = default(Brush);
                brush = new SolidBrush(Color.Blue);

                //Now relocate the points for the triangle.
                pnt[0].X += 100;
                pnt[1].X += 100;
                pnt[2].X += 100;

                gr_graphics.FillPolygon(brush, pnt);
           
        }
           
    }
}


Thursday 23 February 2012

Draw a triangle on the form using C#.NET

It shows how to draw a triangle on the form using C#.NET
Keywords: C#.net, CSharp.NET, triangle, drawing, graphics.

First import a namespace
using System.Drawing;

Place the code in the paint event for the form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testc
{
    public partial class Form1 : Form
    {
        Graphics gr_graphics = default(Graphics);
        //need a pen for drawing and make it black
        Pen pen_draw = new Pen(Color.Black);

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            gr_graphics = e.Graphics;
            //Draw a triangle on the form.
            //first have to define an array of points.
            Point[] pnt = new Point[3];
          
            pnt[0].X = 150;
            pnt[0].Y = 150;

            pnt[1].X = 150;
            pnt[1].Y = 200;

            pnt[2].X = 50;
            pnt[2].Y = 120;

            gr_graphics.DrawPolygon(pen_draw, pnt);
        }   
    }
}