Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi all,

i have an application in c# with a panel and a picture box
the user is able to draw on the panel (using mouseUp, mouseDown and mouseMove events)
and then i need to display what the user drew on the panel, in the picture box (and that's my problem)

my code is:
namespace myWhiteBoardTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static Boolean clck = false;
        public static int currentX;
        public static int currentY;
        public static int oldX;
        public static int oldY;
        Bitmap thePanelImg;
        Pen myP = new Pen(Color.Red, 3);
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            clck = true;
            oldX = e.X;
            oldY = e.Y;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            
            if (clck == true)
            {
                g.DrawLine(myP, oldX, oldY, e.X, e.Y);
                oldX = e.X;
                oldY = e.Y;
            }
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            clck = false;
            panel1.DrawToBitmap(thePanelImg, new Rectangle(0, 0, panel1.Width, panel1.Height));
            pictureBox1.Image = thePanelImg;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
          thePanelImg = new Bitmap(panel1.Width, panel1.Height);
        }


what's happening is that i'm getting only the background of the panel in the pictureBox without the drawn patterns

can anyone help?

thank you
Posted

1 solution

Oh gosh, you do it all wrong! Forms do not persist rendered graphics at all. Here is what to do:

You need to do all your drawing in the overridden method OnPaint or event handler Paint. In both cases, for drawing, use the instance of the System.Drawing.Graphics passed in the event argument parameter. Your rendering method will be called each time WM_PAINT Windows message is dispatched to your window, for example, when you hide and show you form again or when your graphics is masked by some other window and later shown again. Never create an instance of Graphics for your control, always use the one from the event argument parameter. In fact, sometimes you can do it, but it's much more advanced technique. In your case, just don't do it.

What do to do change graphics? You're using some data for rendering. Change this data, that's it. Next time WM_PAINT comes, your new data will be used for re-drawing. But if nothing else is triggered, how to programmatically trigger this WM_PAINT? The only one valid way to do it is calling System.Windows.Forms.Control.Invalidate on the control where you render the graphics. For performance sake, you may want to use Invalidate with a parameter (Rectangle or Region) to invalidate only the part of the scene which really needs update.

To avoid flicker, you also may need double buffering options, see Control.SetStyle. It's protected, so do it in the derived Control class. If this is a Form, you derive it from System.Windows.Forms.Form anyway.

For a code sample, see this: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^].

Just in case, if I missed something, check my past answers (essentially the same):
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^].

[EDIT]

Just in case, some more references to my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^];
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

—SA
 
Share this answer
 
v4
Comments
Maciej Los 10-Feb-13 14:44pm    
Comment from OP:
first of all, thank you for your reply
but unfortunately it's hard for me to understand all of this =(
i'm still a beginner =$
but i'm looking at your links now ,, i'll try to learn it
thank you very much again
Sergey Alexandrovich Kryukov 10-Feb-13 14:50pm    
No, this stuff is not the hardest to understand and use. You just need to start implementing it, perhaps starting from some simplest functionality.
When (and if) you see it's reasonable, don't forget to accept the answer formally (green button).
Anyway, your follow-up questions will be welcome.
—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