Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm drawing on an image, but when the mouse moves out of the image,
so delete or lines I've drawn
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;

namespace MapStore
{
    public partial class Form1 : Form
    {
        #region dailg
        OpenFileDialog OpenFiledialog;
        //FileDialog OpenFiledialog;
     
        #endregion

        #region drwing
        Graphics g;
        Pen p = new Pen(Color.Blue, 2);
        Point strart;
        Point end;

        #endregion
        #region enums
        enum Enum_drwing  
        {
            line,
            recntlage
        }
        #endregion


        Int32 var_draw_case;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
            OpenFiledialog.InitialDirectory =  "c:\\";
            OpenFiledialog.FilterIndex = 1;
            if (OpenFiledialog.ShowDialog() == DialogResult.OK )
            {
                pictureEdit1.Image = Image.FromFile(OpenFiledialog.FileName);

            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureEdit1_MouseMove(object sender, MouseEventArgs e)
        {
            


            if ((var_draw_case == (int)Enum_drwing.line) &&  (e.Button == MouseButtons.Left))
            {
                pictureEdit1.Cursor = Cursors.Cross;
                 

                ControlPaint.DrawReversibleLine(pictureEdit1.PointToScreen(strart), pictureEdit1.PointToScreen(end), Color.Black);

                end = new Point(e.X, e.Y);

                ControlPaint.DrawReversibleLine(pictureEdit1.PointToScreen(strart), pictureEdit1.PointToScreen(end), Color.Black);



            }


            else

            {

                strart = new Point();

            }

        }

     


 
        

        private void BUT_drawline_Click(object sender, EventArgs e)
        {
            var_draw_case = (int)Enum_drwing.line;
        }

        private void pictureEdit1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                if ((var_draw_case == (int)Enum_drwing.line))
                {
                    strart.X = e.X;
                    strart.Y = e.Y;

                }
                else
                {
                    strart = new Point();
                }
               
            }
        }
        
      
    }
}
Posted
Updated 12-Jan-11 1:01am
v2

Espens answer is the best you're going to get.

There's so much wrong with your existing code that its better just to scrap it and start over. Your painting should be done in your controls Paint event, not in a MouseMove.

Using MouseMove is fine for drawing the rubberband line, but once the mouse button is released, you have no code for drawing the final line to a bitmap image that can be used to repaint the control when needed.

Why do you this? Because if another form was dragged over yours, the entire image would be lost. There's no code nor source data at all to repaint your form/controls once they are visible again.
 
Share this answer
 
Here is an article that might be of interest:
OpenS-CAD, a simple 2D CAD application[^]

Not an exact answer, but it does what you probably need to do. (and then some...)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Dalek Dave 12-Jan-11 7:08am    
Good Answer.
Espen Harlinn 12-Jan-11 7:09am    
Thanks Dalek!

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