Tuesday, February 21, 2012

Drawing Simple Shapes in C# in Windows Form Application

This is a very basic tutorial regarding drawing simple shapes in C# in visual studio.
First goto File->New->Project
Select Visual C# and Windows Form Application and give a name to your project.

This must be your screen.
Now on the rightmost bar, select the toolbar and click on the picturebox and click back to the form window.You will get the picture box and adjust the size of box by dragging.
Now go back to toolbar and get the button similar to picture box.
Double click the button and put the sample code in it.

private void button1_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawLine(pen1, 250, 50, 400, 200);
        }

Repeat the process and your code will be like:
//form1.cs
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;
using System.Windows;



namespace draw_shapes
{
    public partial class Form1 : Form, IDisposable
    {
        private Bitmap m_Canvas;
        public Form1()
        {
            InitializeComponent();
        }

        private System.Drawing.Graphics g;
        private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Black, 2F);
       



        private void button1_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawLine(pen1, 250, 50, 400, 200);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawEllipse(pen1, 50, 50, 100, 150);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawRectangle(pen1, 30, 30, 50, 60);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawArc(pen1, 150, 100, 150, 200, 150, 160);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawPie(pen1, 50, 50, 150, 150, 0, 170);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Drawing.Point[] p = new System.Drawing.Point[6];
            p[0].X = 0;
            p[0].Y = 0;
            p[1].X = 53;
            p[1].Y = 111;
            p[2].X = 114;
            p[2].Y = 86;
            p[3].X = 34;
            p[3].Y = 34;
            p[4].X = 165;
            p[4].Y = 7;
            g = PictureBox1.CreateGraphics();
            g.DrawPolygon(pen1, p);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            g = PictureBox1.CreateGraphics();
            g.DrawBezier(pen1, 100, 200, 240, 250, 100, 200, 150, 30);
        }

       

        private void button9_Click(object sender, EventArgs e)
        {
            m_Canvas = new Bitmap(700, 700); // Doesn't have to be initialized here

            for (int x = 100; x < 200; x++)
            {
                for (int y = 100; y < 200; y++)
                {
                    m_Canvas.SetPixel(x, y, Color.Blue);
                }
            }
            SetCanvasAsImage();
        }

        public void SetCanvasAsImage()
        {
            PictureBox1.Image = m_Canvas;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            PictureBox1.Refresh();
        }

        private void button10_Click(object sender, EventArgs e)
        {

        }

     
        }
    }