-
August 4th, 2010, 12:07 PM
#1
Draw lines over image in picturebox
Hi all,
I want to draw lines over an existing image in a picturebox (VS2008, .net3.5). No issues as far as drawing it once is concerned. But I want these lines to be shifted to the mouse position as I move in the picture box. So I wrote the following code in the PictureBox_MouseMove event:-
{
Graphics g = pictureBox1.CreateGraphics();
g.Clear(Color.Transparent);
Pen p = new Pen(Color.Red, 2.0f);
for (int n = 1; n <= 50; n++)
{
g.DrawLine(p, n * (Cursor.Position.X), Cursor.Position.Y - 30.0f, n * (Cursor.Position.X), Cursor.Position.Y + 30.0f);
}
The code is working fine when there is no image in the PictureBox. Whern there is an image in the picturebox, the lines are getting drawn, but before that the image already in the picturebox is getting erased (g.Clear(Color.Transparent)).
I tried to keep the image in a panel and run the code in Panel_MouseMove event, but to no avail.
How do I draw these lines while keeping the existing image intact?
GeoNav
-
August 4th, 2010, 12:11 PM
#2
Re: Draw lines over image in picturebox
Using CreateGraphics is a bad idea. You should be doing all of your drawing in the Paint handler.
-
August 5th, 2010, 04:56 AM
#3
Re: Draw lines over image in picturebox
Originally Posted by BigEd781
Using CreateGraphics is a bad idea. You should be doing all of your drawing in the Paint handler.
Does it mean that I can draw these lines only in the pain event and not recommended to draw it in the MouseMove event?
Since I want to show these lines when the moue enters the PIcture box, can you suggest a walk around?
-
August 5th, 2010, 12:41 PM
#4
Re: Draw lines over image in picturebox
You don't need a workaround. All that you need to do is save the necessary data in the mouse move event and then call Invalidate(). Your paint event will take over from there.
-
August 6th, 2010, 04:16 AM
#5
Re: Draw lines over image in picturebox
Originally Posted by BigEd781
You don't need a workaround. All that you need to do is save the necessary data in the mouse move event and then call Invalidate(). Your paint event will take over from there.
If it is not asking much, can you show a sample?
-
August 6th, 2010, 02:34 PM
#6
Re: Draw lines over image in picturebox
Code:
Point m_lastMouseLoc;
void _MouseMove( object sender, MouseEventArgs e )
}
m_lastMouseLoc = e.Location;
Invalidate();
}
void _Paint( object sender, PaintEventArgs e )
{
using( Pen p = new Pen(Color.Red, 2.0f) )
{
// draw the line using m_lastMouseLoc instead of Cursor.Position as the reference point.
}
}
I would add that:
1. You are not disposing of your Pen object as you should be.
2. If you are going to draw many lines it is better to use Graphics.DrawLines than to use DrawLine in a loop like that.
3. Using hard-coded values like "30.0" is typically a bad sign. Sometimes it is necessary, but not often.
4. (IMPORTANT) drawing to the screen in MouseMove is generally a bad idea. You will be drawing so often that you will really be taxing the machine and you will likely see a lot of flickering. Drawing to the screen is an expensive operation, you should be a little smarter about when and how you do it.
-
August 7th, 2010, 02:57 PM
#7
Re: Draw lines over image in picturebox
Hi BigEd781,
Thanks a lot for the suggestions and advices. You made my life so much simpler.
Originally Posted by BigEd781
2. If you are going to draw many lines it is better to use Graphics.DrawLines than to use DrawLine in a loop like that.
I am trying to measure the distances between different lines dynamically presented as result of Fast Fourier Transform of audio picked up in the microphone. So I have to change the distance between the lines as the multiple of distance of the mouse from the left edge of the picturebox. If I use DrawLInes, the line will be continuous as I understand.
3. Using hard-coded values like "30.0" is typically a bad sign. Sometimes it is necessary, but not often.
I wanted to keep the line length same all through, so I thought it is better to specify it.
4. (IMPORTANT) drawing to the screen in MouseMove is generally a bad idea. You will be drawing so often that you will really be taxing the machine and you will likely see a lot of flickering. Drawing to the screen is an expensive operation, you should be a little smarter about when and how you do it.
For the given requirement, I could not think of any other way. Can you suggest some other method?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|