Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code

C#
private void drawP_MouseDown(object sender, MouseEventArgs e)
      {
          if (!drawbool)
          {
              dStartPoint = e.Location;
              drawbool = true;
          }
          drawP.Invalidate();
      }

      private void drawP_MouseMove(object sender, MouseEventArgs e)
      {
          if (drawbool)
          {
              
              dStartPoint = e.Location;//dStartPoint is a point
              drawP.Invalidate();
          }
      }

C#
private void drawP_MouseUp(object sender, MouseEventArgs e)
       {
           if (drawbool)
           {
               drawbool = false;
               
           }
       }

C#
private void drawP_Paint_1(object sender, PaintEventArgs e)
       {
           if (drawbool)
           {
               int dStartX = dStartPoint.X;
               int dStartY = dStartPoint.Y;


               

               e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);//dP is a Pen



               Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
               
       drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
               drawP.BackgroundImage = (Image)dPPB;
                }
           }
Posted
Updated 7-Dec-11 22:33pm
v2
Comments
Smithers-Jones 8-Dec-11 4:38am    
The problem is, that you are too impatient. You asked your question just about an hour ago. Have patience and you might get an answer, be impatient and urge people and you just put possible helpers off.

modified: OP posted a comment asking why nobody is answering his question, on which I answered with this comment. Now he deleted his comment which makes mine look pretty stupid. "Thank you very much."
ready to learn 8-Dec-11 5:27am    
Sorry, I deleted it because I got my answer and I didn't want to extand the question by the sentence not related to it.You could have deleted your comment too.Any way to avoid making you angry,I modified the comment.
Sergey Alexandrovich Kryukov 8-Dec-11 11:31am    
You should do exactly that -- expand your answer and post appropriate comments using "Improve question".
--SA

1 solution

You approach is almost correct, but you should fix your logic and problems in the code. In brief, you should do all rendering in your handler of your Paint event. First, you draw the background image, and, on top if it, you draw your mouse-generated line from data. The mouse events only manipulate data and call Invalidate.

Your performance can be improved by calling Invalidate with a parameter (Rectangle or Region) — it will trigger re-rendering of only the part of the scene.

Also, I must say you don't need the Panel. You should better derive a custom control from Control and override OnPaint instead of using Paint event. Don't worry, you will need to resort to a derived control anyway. Why? Because you also need to add double buffering, but to do that, you will need to access the protected method SetStyles.

You will need to add the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer. This is important to avoid flicker.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^].

You also need to fix your code style, naming, etc.

—SA
 
Share this answer
 
v6
Comments
ready to learn 8-Dec-11 13:30pm    
Thanks a lot.your answer is helpfull.I found an article which _I think_ provides the codes of what you explain.
Sergey Alexandrovich Kryukov 8-Dec-11 13:47pm    
If so, please also accept this answer formally (green button).
Good luck,
--SA
Monjurul Habib 8-Dec-11 15:32pm    
nice explanation with links,5!
Sergey Alexandrovich Kryukov 8-Dec-11 16:06pm    
Thank you, Monjurul.
--SA

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