Click here to Skip to main content
15,662,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I get x and y coordinates of a point in an image in a picturebox by mouse move event but when I draw a line with them the line is drawn in another point.why?

private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
          toolStripStatusLabel2.Text = "X:" + e.X + "," + "Y:" + e.Y;

private void button2_Click(object sender, EventArgs e)


pictureBox2.Height = 3152;
pictureBox2.Width = 4760;
Graphics g = Graphics.FromImage(pictureBox2.Image);
Pen p = new Pen(Color.Black,9);

Updated 2-Jul-11 4:01am
Shahin Khorshidnia 2-Jul-11 8:59am    
Windows, WPF ?
I must read the code too.
Member 7965041 2-Jul-11 9:37am    
windows form.
Is it possible : the point that I get from mouse differs from the point of my image?
Sergey Alexandrovich Kryukov 2-Jul-11 20:09pm    
No! Add tag "Forms" to your question. Some code?

Drawing interactively on PictureBox makes little sense, as most of the thing people try to do with this component. You really need to draw on custom control (derived from System.Windows.Forms.Control), Panel, even on Form, but not on PictureBox. You can even save the image after interactive drawing if you abstract out the rendering method (by drawing on other media such as System.Drawing.Bitmap and saving it).

To read about how to do it, please see my past answer:
How do I clear a panel from old drawing[^].

Share this answer
VallarasuS 3-Jul-11 3:26am    
The painting actually happens with-in the bitmap context, and is most widely used to create complex bitmaps!, PictureBox just being invalidated when required. (ting)!! Do you think is necessary to extend / create a custom control?
Sergey Alexandrovich Kryukov 3-Jul-11 3:41am    
No! I explained it in the answer references.

One can use Panel or Form. Well, "extend", a big deal!
You need to sub-class a control to switch on double buffering, that's pretty much it. PictureBox is the worse solution. It is used to create bitmaps, yes, but only static ones. If you want to move some objects within it, it's the evil. Come on, so many projects are done correctly, no one uses the PictureBox for rendering anything interactive or animated. It only creates extra hassles (like the one OP is having now), adds no value at all.

There are many answers on CodeProject discouraging doing it.
the event args might be in screen co-ordinates, you should convert them into client co-ordinates before using them.

for instance,

Point newPt = pictureBox.PointToClient(point);

Hope this helps.
Share this answer
RaviRanjanKr 2-Jul-11 14:28pm    
Nice Suggestion, My 5 :)
Sergey Alexandrovich Kryukov 2-Jul-11 20:16pm    
Well, let's get to the root of the problem: PictureBox should not be used.
Please see my solution.
You may compare with this VB.NET (tested) and its C# analog (not tested) drawing by mouse program.
Share this answer
Sergey Alexandrovich Kryukov 2-Jul-11 20:15pm    
It makes little sense (I did not vote).
Well, let's get to the root of the problem: PictureBox should not be used.
Please see my solution.

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