Hi every one!
i want to make a drawing programm in c# like ms paint in windows. I want to draw line, rectangle, square, ellipse and other objects on mouse move and mouse up events and fill them with colour.
To do this from where i have to start? any tutorials or ebook or sample projects. pls rply

Recommended Answers

All 9 Replies

Start simple. Can you draw a line, rectangle a circle on a form? If not, try to do that first.

thnx for ur kind response.
i can draw line, rectangle, circle and every thing(fixed size) on form. but i have no idea to draw these object on mouse events.(up, down and move)
once again thanx.

Here is some code I am still working on.
It shows how to draw a rectangle during a mousemove, while the left mouse button is still down.

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ScreenCapture
{
    public partial class TransparantFoil : Form
    {
        private Form _master;
        private Point _FirstClickPt;
        private Point _TopLeftPt, _BottomRightPt;
        private Graphics _G;
        private Pen _Pen;
        private Pen _Erase;

        public TransparantFoil() {}

        public TransparantFoil(Form M)
        {
            InitializeComponent();
            _master = M;
            _FirstClickPt = new Point();
            _TopLeftPt = new Point();
            _BottomRightPt = new Point();
            _G = CreateGraphics();
            _Pen = new Pen(Color.DarkBlue);
            _Erase = new Pen(this.BackColor);
            //(Cursor)(ScreenCapture.Properties.Resources.viser);// myCursor;
        }
   
        private void TransparantFoil_MouseDown(object sender, MouseEventArgs e)
        {
            _FirstClickPt.X = e.X;
            _FirstClickPt.Y = e.Y;
        }

        private void TransparantFoil_MouseMove(object sender, MouseEventArgs e)
        {           
            if (e.Button == MouseButtons.Left) //only do something if left mouse btn clicked
            {
                //Erase previous rectangle
                _G.DrawRectangle(_Erase, _TopLeftPt.X, _TopLeftPt.Y, 
                    _BottomRightPt.X - _TopLeftPt.X, _BottomRightPt.Y - _TopLeftPt.Y);

                //Determine X coordinates
                if (Cursor.Position.X < _FirstClickPt.X)    //moved left
                {
                    _TopLeftPt.X = Cursor.Position.X;
                    _BottomRightPt.X = _FirstClickPt.X;
                }
                else
                {
                    _TopLeftPt.X = _FirstClickPt.X;
                    _BottomRightPt.X = Cursor.Position.X;
                }

                //Determine Y coordinates
                if (Cursor.Position.Y < _FirstClickPt.Y)    //moved up
                {
                    _TopLeftPt.Y = Cursor.Position.Y;
                    _BottomRightPt.Y = _FirstClickPt.Y;
                }
                else
                {
                    _TopLeftPt.Y = _FirstClickPt.Y;
                    _BottomRightPt.Y = Cursor.Position.Y;
                }

                //Draw a new rectangle
                _G.DrawRectangle(_Pen, _TopLeftPt.X, _TopLeftPt.Y,
                    _BottomRightPt.X - _TopLeftPt.X, _BottomRightPt.Y - _TopLeftPt.Y);
            }
        }

        private void TransparantFoil_MouseUp(object sender, MouseEventArgs e)
        {
            this.Hide();        //hide our foil
            DialogResult result = saveFileDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
               SaveCapture(saveFileDlg.FileName);
            }          
            _master.Show();     //show the master control panel again
        }

        private void TransparantFoil_FormClosing(object sender, FormClosingEventArgs e)
        {
            _G.Dispose();
        }

        private void SaveCapture(string Fname)
        {
            Rectangle ImageBounds = new Rectangle(_TopLeftPt.X, _TopLeftPt.Y,
                _BottomRightPt.X - _TopLeftPt.X, _BottomRightPt.Y - _TopLeftPt.Y);
            Bitmap bitmap = new Bitmap(ImageBounds.Width, ImageBounds.Height);
            Graphics G = Graphics.FromImage(bitmap);    //Create Graphics object from the bitmap
            G.CopyFromScreen(_TopLeftPt, Point.Empty, ImageBounds.Size);
            bitmap.Save(Fname, ImageFormat.Bmp);
        }
    }
}
commented: Nice code...very helpful post. +7

thnx it is enough for basic. but what i have to do if i want to resize, select, move or fill any drawn object.
thnx

if you want to be able to alter something after it is drawn you will need to store each object you draw in some way. I would recommend creating some classes. Create a base class for drawn objects that stores common details (location, colour, etc) then create some subclasses which inherit from that class to store details specific to different types of object:

public class DrawnItem
    {
        public Color color { get; set; }
        public Point location { get; set; }
        //etc
    }
    public class DrawnCircle : DrawnItem
    {
        public int radius { get; set; }
        //etc
    }

Each time you draw a new item, add it to a List. That way you have a record of the items you have added to the drawing area.

If you dont maintain a record of each item you will have no way to distinguish them ocne they are drawn to the screen.

thank You.
Explain some thing more about this
or any example please.
Once again thank

Hi,

I need to draw lines in a C# web form. How do i do it?

@poifull do not resurrect old threads! Start a new one with your question, and of course you may always refer to this one.

Thanks @poifull and @danny.

@poifull has created a new thread.

Thread locked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.