Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a paint like application. I want user to draw on a panel and then save whats on it. I found this code online. It works on form itself perfectly but when i convert it to the code down below to use it on a panel it does not show anything and it does not gives any errors.

C#
List drawingRects = new List();

        public class DrawingRectangle
        {
            public Point Location { get; set; }
            public Size Size { get; set; }
            public Point StartPosition { get; set; }
            public Color DrawingcColor { get; set; } = Color.LightGreen;
            public float PenSize { get; set; } = 3f;

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (drawingRects.Count == 0) return;
            //e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            foreach (var rect in drawingRects)
            {
                using (Pen pen = new Pen(rect.DrawingcColor, rect.PenSize))
                {
                    e.Graphics.DrawRectangle(pen, new Rectangle(rect.Location, rect.Size));
                };
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DrawingRectangle rect = drawingRects.Last();
                if (e.Y < rect.StartPosition.Y) { rect.Location = new Point(rect.Location.Y, e.Y); }
                if (e.X < rect.StartPosition.X) { rect.Location = new Point(e.X, rect.Location.Y); }

                rect.Size = new Size(Math.Abs(rect.StartPosition.X - e.X), Math.Abs(rect.StartPosition.Y - e.Y));
                drawingRects[drawingRects.Count - 1] = rect;
                this.Invalidate();
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                drawingRects.Add(new DrawingRectangle()
                {
                    Location = e.Location,
                    Size = Size.Empty,
                    StartPosition = e.Location
                });
            }
        }


What I have tried:

I don't know what is wrong with this code and could not find anything online but i am still researching.
Posted
Updated 13-Aug-20 4:09am

1 solution

Start by using the debugger to check exactly what is happening: put a breakpoint at the start of each method and run your app.
You should almost immediately get a Paint event so remove or disable it, continue the app, and then put it back (making sure you don't invalidate the app form with VS).
Then hold the mouse button down ready to drag. Did that event happen? If so, follow the code through and make sure all your data looks right. Remove or disable the breakpoint temporarily.
Continue your app and do it again, this time hold and drag.
You should get the third method breakpoint firing. follwo teh code through, make sure it all looks right.
Continue your app.
Did you get another Paint event for your Panel?

If not, change
this.Invalidate();
to
panel1.Invalidate();
and try again.
 
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