Draw lines over image in picturebox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Draw lines over image in picturebox

  1. #1
    Join Date
    Aug 2006
    Posts
    93

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Aug 2006
    Posts
    93

    Re: Draw lines over image in picturebox

    Quote Originally Posted by BigEd781 View Post
    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?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  5. #5
    Join Date
    Aug 2006
    Posts
    93

    Re: Draw lines over image in picturebox

    Quote Originally Posted by BigEd781 View Post
    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?

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  7. #7
    Join Date
    Aug 2006
    Posts
    93

    Re: Draw lines over image in picturebox

    Hi BigEd781,
    Thanks a lot for the suggestions and advices. You made my life so much simpler.
    Quote Originally Posted by BigEd781 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center