0

I want to draw a rectangle, triangle, circle and a pie form graphic using draw and drag with the mouse (not filled).

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.Runtime.InteropServices;
        using System.Drawing.Imaging;
        using System.IO;

        namespace mainpaint
        {
            public partial class Form1 : Form
            {
                Color paintcolor;
                bool choose = false;

                bool draw = false;
                int x, y, lx, ly = 0;
                Item currItem;
                public Form1()
                {
                    InitializeComponent();
                    this.DoubleBuffered = true;
                    this.Cursor = System.Windows.Forms.Cursors.Cross;

                }

                public enum Item
                {
                    Rectangle, Ellipse, Line,Pencil,eraser, Triangle
                }

                private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
                {
                    choose = true;
                }

                private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
                {
                    choose = false;
                }

                private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
                {

                    if (choose)
                    {

                        Bitmap bmp = (Bitmap)pictureBox2.Image.Clone();
                        paintcolor = bmp.GetPixel(e.X, e.Y);
                        red.Value = paintcolor.R;
                        green.Value = paintcolor.G;
                        blue.Value = paintcolor.B;
                        alpha.Value = paintcolor.A;
                        redval.Text = paintcolor.R.ToString();
                        greenval.Text = paintcolor.G.ToString();
                        blueval.Text = paintcolor.B.ToString();
                        alphaval.Text = paintcolor.A.ToString();
                        pictureBox3.BackColor = paintcolor;

                    }
                }

                private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
                {
                    draw = true;
                    x = e.X;
                    y = e.Y;
                }

                private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
                {
                    draw = false;
                    lx = e.X;
                    ly = e.Y;

                    if (currItem == Item.Line)
                    {
                        Graphics g = pictureBox1.CreateGraphics();
                        g.DrawLine(new Pen(new SolidBrush(paintcolor),3), new Point(x, y), new Point(lx, ly));
                        g.Dispose();

                    }
                }

                private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
                {
                    if (draw)
                    {
                        Graphics g = pictureBox1.CreateGraphics();



                        switch (currItem)
                        {
                            case Item.Rectangle:


                                g.DrawRectangle(new Pen(new SolidBrush(paintcolor),3), x, y, e.X - x, e.Y - y); 

                               // g.FillRectangle(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y); these is to draw a filled rectangle not a problem
                                break;
                            case Item.Ellipse:
                                g.DrawEllipse(new Pen(new SolidBrush(paintcolor), 3), x, y, e.X - x, e.Y - y);
                                //g.FillEllipse(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y);
                                break;
                            case Item.Pencil:
                                g.FillEllipse(new SolidBrush(paintcolor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
                                break;
                            case Item.eraser:
                                g.FillEllipse(new SolidBrush(pictureBox1.BackColor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
                                break;
                            case Item.Triangle:
                                 g.FillPolygon(new SolidBrush(paintcolor), new Point[3] { new Point(x-e.X,y+e.Y), new Point(x,y), new
                                   Point(x+e.X,y+e.Y) });

                                break;


                        }
                        g.Dispose();
                    }
                }


                private void toolStripButton9_Click(object sender, EventArgs e)
                {
                    currItem = Item.Rectangle;
                }

                private void toolStripButton10_Click(object sender, EventArgs e)
                {
                    currItem = Item.Ellipse;
                }

                private void toolStripButton13_Click(object sender, EventArgs e)
                {
                    currItem = Item.Pencil;
                }

                private void toolStripButton15_Click(object sender, EventArgs e)
                {
                    currItem = Item.eraser;
                }

                private void toolStripButton11_Click(object sender, EventArgs e)
                {
                    currItem = Item.Line;
                }

           private void toolStripButton6_Click(object sender, EventArgs e)
                {
                    currItem = Item.Triangle;
                }

                private void Form1_Load(object sender, EventArgs e)
                {
                        FontFamily[] family = FontFamily.Families;

                }

                private void red_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    redval.Text = "R: " + paintcolor.R.ToString();
                }

                private void green_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    greenval.Text = "G: " + paintcolor.G.ToString();
                }

                private void blue_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    blueval.Text = "B: " + paintcolor.B.ToString();
                }

                private void alpha_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    alphaval.Text = "A: " + paintcolor.A.ToString();
                }



            }
        }

Im having these results:

Rectangles and Circles:

enter image description here

And my triangle doesnt even have a form (im using the filling method here whith polygon,the draw method should be the same?)

enter image description here

And i want something like this:

enter image description here

And finally, how can I draw a pie form figure?

Thank you!

user3522262
  • 79
  • 1
  • 3
  • 8
  • 1
    Do have a look at my comments [here](http://stackoverflow.com/questions/28907231/getting-a-line-that-has-the-coordinates-defined-by-the-mouse-location?noredirect=1#comment46082800_28907231)! Also at [this post](http://stackoverflow.com/questions/28830821/preserve-painting-after-resize-or-refresh/28834298#28834298) - A pie can be drawn by drawing two lines and one Arc. `DrawArc` reads complicated but it is not! - Be prepare to rewrite all your code!! – TaW Mar 09 '15 at 00:05
  • The problem here is you are drawing objects again and again without removing already temporarily added object. Either redraw the previous object with picture box background color or add the paint event draw the object in paint. – Razack Mar 09 '15 at 04:20

0 Answers0