Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Every body!
I'm writing an application: Load photos from PC then displayed on the picturebox, namely:
  When the user presses F1, a line will be drawn up at the picture that the mouse coordinates are pointing to. My problem is when the user presses F1 again, the line will be drawn initially deleted instead new line will be drawn at the new location.
I also refer to some links but no solutions. We wish all the suggestions and help so that I can complete my project.
A link has reference number:
1. http://social.msdn.microsoft.com/Forums/windows/en-US/4dd351f7-f37f-45d9-8c34-50c1b52b27df/undo-and-redo-lines-on-the-image-drawn-by-user-in-picture-box-of-csharp-windows-form?forum=winforms[^]
Thank you all very much
Posted
Comments
BillWoodruff 9-Oct-14 7:26am    
A line drawn "up at the picture" from where ?

Assuming you are drawing the lines in the Paint Event, and you want the lines to persist, you are going to need to maintain a list of lines to draw and re-draw them.

Post carefully selected examples from your actual code showing where you are stuck.
ZurdoDev 9-Oct-14 8:06am    
You'll need to fix the code then.

1 solution

Hi BillWoodruff! Thanks for your answer.
Line is drawn from the following functions, this function is called in the keydown event of the form.
public void DrawLine(PictureBox pb, Point p, Color color)
        {
            try
            {
                Point p1 = new Point();
                Point p2 = new Point();
                Point p3 = new Point();
                Point p4 = new Point();
                //Draw as plus sign
                p1.X = p.X;
                p1.Y = p.Y - 10;
                p2.X = p.X;
                p2.Y = p.Y + 10;
                p3.X = p.X - 10;
                p3.Y = p.Y;
                p4.X = p.X + 10;
                p4.Y = p.Y;
                pb.Refresh();
                Bitmap bmp = (Bitmap)pb.Image;
                Graphics g = Graphics.FromImage(bmp);
                Pen pe = new Pen(color, 3);
                g.DrawLine(pe, p1, p2);
                g.DrawLine(pe, p3, p4);
                pb.Image = bmp;
                pe.Dispose();
                g.Dispose();
            }
            catch
            {
                MessageBox.Show("Please select image let draw!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

And when the user draw a new line, the line has been drawn previously been removed.
 
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