Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need your help for adding Rectangle,Circle and Ellipse options to my mini-paint app project,but i need all this on mouse event, i searched every where but most of sites give specific dimensions so I'm failed to get rectangle option to work :(

i add save option to save the drawn pic to bmp file but it always save a blank file :(

this is my code :
C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    public partial class Form1 : Form
    {
        Graphics Graphic;
        Pen p = new Pen(Color.Black, 2);
        Point ep = new Point(0, 0);
        Point sp = new Point(0, 0);


        int k = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            p.Color = pinc.BackColor;
            recentcolor.BackColor = pinc.BackColor;
        }

        private void rose_Click(object sender, EventArgs e)
        {
            p.Color = rose.BackColor;
            recentcolor.BackColor = rose.BackColor;
        }

        private void blue_Click(object sender, EventArgs e)
        {
            p.Color = blue.BackColor;
            recentcolor.BackColor = blue.BackColor;
        }

        private void violet_Click(object sender, EventArgs e)
        {
            p.Color = violet.BackColor;
            recentcolor.BackColor = violet.BackColor;
        }

        private void yellow_Click(object sender, EventArgs e)
        {
            p.Color = yellow.BackColor;
            recentcolor.BackColor = yellow.BackColor;
        }

        private void green_Click(object sender, EventArgs e)
        {
            p.Color = green.BackColor;
            recentcolor.BackColor = green.BackColor;
        }

        private void red_Click(object sender, EventArgs e)
        {
            p.Color = red.BackColor;
            recentcolor.BackColor = red.BackColor;
        }

        private void orange_Click(object sender, EventArgs e)
        {
            p.Color = orange.BackColor;
            recentcolor.BackColor = orange.BackColor;
        }

        private void white_Click(object sender, EventArgs e)
        {
            p.Color = white.BackColor;
            recentcolor.BackColor = white.BackColor;
        }

        private void black_Click(object sender, EventArgs e)
        {
            p.Color = black.BackColor;
            recentcolor.BackColor = black.BackColor;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            Close();             // Exit 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;  // Clear Image

        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
            using (var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))
            {
                pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                bmp.Save(@"c:\test.png");
            }

        }

        private void pictureBox1_MouseDown_1(object sender, MouseEventArgs e)
        {
            sp = e.Location;
            if (e.Button == MouseButtons.Left)
                k = 1;
        }

        private void pictureBox1_MouseUp_1(object sender, MouseEventArgs e)
        {
            k = 0;
        }

        private void pictureBox1_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (k == 1)
            {
                ep = e.Location;

                Graphic = pictureBox1.CreateGraphics();
                Graphic.DrawLine(p, sp, ep);
                sp = ep;

            }
        }

    }
}
Posted

1 solution

To draw items like rectangles, circles, etc. you need to first think about how you want your user to interact with the program when the are adding a rectangle, etc. For example - mouse down, drag, up might record one corner (mouse down position) and then the opposite corner (mouse up). A circle might use the mouse-down as a corner of a bounding rectangle - or it might use the mouse down as the center location.

In general, then, you need to detect mouse down and mouse up and, provided that they are both over your picture box, the locations of the down and up events become control locations for the graphic object your program is drawing.

Note - the coordinates you get when handling the mouse events may be form, or screen, based. If so, you'll need to translate them into positions within your picture box and, further, you may need to translate them into bitmap pixel positions (a 100x100 pixel picture box may be displaying an image many times larger or smaller than that size).

Chris
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900