Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# image
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 25-May-11 11:15am
Saba_TH179

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
Maciej Los at 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 at 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)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100