Monday, June 9, 2014

Basic Graphics Drawing Shape




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D; //Added to draw linear Gradient brush
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GraphicPractice_2
{
    public partial class Form1 : Form
    {
        Bitmap bmap;
        Graphics g;
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
        }

        //method to get Random Point
        private Point GetRandomPoint(Bitmap bmap)
        {
            int x = rand.Next(bmap.Width);
            int y = rand.Next(bmap.Height);
            Point p = new Point(x, y);
            return p;
        }

        //method to get Random Color
        private Color GetRandomColor()
        {
            int red = rand.Next(256);
            int green = rand.Next(256);
            int blue = rand.Next(256);

            Color c = Color.FromArgb(red, green, blue);
            return c;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
            pictureBox1.Image = bmap;
        }

        //DrawLine
        private void btnDrawLines_Click(object sender, EventArgs e)
        {
            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 2);
            Point startPoint = GetRandomPoint(bmap);
            Point endPoint = GetRandomPoint(bmap);
            g.DrawLine(myPen, startPoint, endPoint);
            pictureBox1.Image = bmap;
        }

        //Draw Gradient Line
        private void btnGradientLine_Click(object sender, EventArgs e)
        {
            Color color1 = GetRandomColor();
            Color color2 = GetRandomColor();
           
            Point startPoint = GetRandomPoint(bmap);
            Point endPoint = GetRandomPoint(bmap);

            LinearGradientBrush brush = new LinearGradientBrush(startPoint, endPoint, color1, color2);
            Pen myPen = new Pen(brush);
            g.DrawLine(myPen, startPoint, endPoint);
            pictureBox1.Image = bmap;
        }

        //Draw Rectangle
        private void btnDrawRectangles_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 2);
            g.DrawRectangle(myPen, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        //Fill Rectangle
        private void btnFillRectangle_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);
            g.FillRectangle(brush, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        //Draw Ellipse
        private void btnDrawEllipse_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 2);
            g.DrawEllipse(myPen, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        //Fill Ellipse
        private void btnFillEllipse_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);
            g.FillEllipse(brush, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        //Draw Circle
        private void btnDrawCircle_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);
            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 2);
            g.DrawEllipse(myPen, x, y, diameter, diameter);
            pictureBox1.Image = bmap;
        }
       
        //Fill Circle
        private void btnFillCircle_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);
            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);
            g.FillEllipse(brush, x, y, diameter, diameter);
            pictureBox1.Image = bmap;
        }

        //Draw Pie
        private void btnDrawPie_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            int width = 10 + rand.Next(bmap.Width - x);
            int height = 10 + rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);

            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 1);

            int startAngle = rand.Next(91);
            int sweepAngle = rand.Next(25, 180);

            g.DrawPie(myPen, x, y, diameter, diameter, startAngle, sweepAngle);
            pictureBox1.Image = bmap;
        }

        //Fill Pie
        private void btnFillPie_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            int width = 10 + rand.Next(bmap.Width - x);
            int height = 10 + rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);

            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);

            int startAngle = rand.Next(91);
            int sweepAngle = rand.Next(25, 180);

            g.FillPie(brush, x, y, diameter, diameter, startAngle, sweepAngle);
            pictureBox1.Image = bmap;
        }

        private void btnDrawTriagle_Click(object sender, EventArgs e)
        {
            Point p1 = GetRandomPoint(bmap);
            Point p2 = GetRandomPoint(bmap);
            Point p3 = GetRandomPoint(bmap);
            Point[] points = { p1, p2, p3 };

            Color color = GetRandomColor();
            Pen myPen = new Pen(color, 1);
            g.DrawPolygon(myPen, points);

            pictureBox1.Image = bmap;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

No comments:

Post a Comment