views:

38

answers:

2

Hi,

My current code allows me to draw rectangles from a user defined spot but not in the way in whihc i desire. I need it to be like you would do it in paint, here is my current code:

namespace SimpleDraw2 { /// /// Description of MainForm. /// public partial class MainForm : Form { bool IsMouseDown = false; Point MousePosition; int DrawShape = 0; Bitmap StoredImage;

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
        pictureBox1.Image = new Bitmap    (pictureBox1.Width,pictureBox1.Height);                      
        StoredImage =  new Bitmap(pictureBox1.Width,pictureBox1.Height);
    }

    void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        MousePosition = e.Location;
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        gStored.DrawImage(pictureBox1.Image, 0, 0);
    }

    void PictureBox1MouseUp(object sender, MouseEventArgs e)
    {
        IsMouseDown = false;
    }

    void PictureBox1MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        if (DrawShape == 0) 
        {
            Pen p = new Pen(Color.Red, 10);
            if (IsMouseDown) 
            {
                g.DrawLine(p,MousePosition,e.Location);
                MousePosition = e.Location;
            }
        }
        if (DrawShape == 1) 
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage,0,0);
            g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

        }
        if (DrawShape == 2)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y);
        }
        if (DrawShape == 3)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X);
        }
        //if (DrawShape == 4)
        //{
        //    g.Clear(Color.Transparent);
        //    g.DrawImage(StoredImage, 0, 0);
        //    g.DrawPolygon(Pens.Indigo, Point[] e.X);
        //}

        this.Refresh();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            axWindowsMediaPlayer1.URL = ofd.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
        Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
        Graphics gfx = Graphics.FromImage(bmp);
        gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.BackgroundImage = bmp;
        //axWindowsMediaPlayer1.Visible = false;
        //pictureBox1.Visible = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage);
        gg.Clear(Color.Transparent);
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        g.Clear(Color.Transparent);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        DrawShape = 1;

    }

    private void button6_Click(object sender, EventArgs e)
    {
        DrawShape = 2;
    }

    private void button8_Click(object sender, EventArgs e)
    {
       DrawShape = 3;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        DrawShape = 0;
    }
}

}

If someone could help me edit my code to iron out the issue to make it easy drag and draw system it would me much appreciate.

Thanks in Advance

Chris

A: 

From msdn:

Draws a rectangle specified by a coordinate pair, a width, and a height.

So your code won't work:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

Should be something like

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y));
Carra
The same problem still arises, it saves the last location and draws a square from the conrer of where it last left the picturebox.
Chris Bacon
+1  A: 

The biggest problem I see is that you're trying to draw in the mouse events. This means your drawing will be wiped away the instant you get a refresh event.

Only draw in Paint events, never in mouse events. If you want your app to draw as a result of mouse events, set a point, rectangle, or whatever in the mouse events (like you start to do with IsMouseDown), invalidate the area you want to change in your MouseMoved event, then draw your rectangle or whatever in your Paint event.

Dour High Arch