CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    Question Problem with CreateGraphics()

    Hello all,

    I am making a dynamic graph which is shown on the "form" everytime when I receive some character from serial port.

    To do this. I create graph layout by overriding OnPaint method:

    Code:
            protected override void OnPaint(PaintEventArgs e)
            {
                Graphics gPlot = e.Graphics;
                DrawAxis(gPlot, coordinates..);
                gPlot.Dispose();
            }
    
    //On receiving character, call updateGraph method
    
            private void UpdateGraph(int aData)
            {
                PointF currentPoint = TranslateValueToPixel(aData); //I receive pixel point to be drawn 
                Graphics aGPlot = this.CreateGraphics();
                aGPlot.DrawLine(Pens.Green, lastPoint, currentPoint);
                lastPoint = currentPoint;
                aGPlot.Dispose();  //have tried without disposing too
            }
    My problem is that it works well if the form is on the screen (in scope) but if it becomes out of scope, the graph refreshes itself, which means all the points that I have drawn on the graph are lost (although graph itself that was created with OnPaint() remains) and it starts drawing of new aData. The problem is clear and I understand it too but is there any solution to it?

    Regards,
    Ricky

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Problem with CreateGraphics()

    well, you should not split your graph drawing; one part doing on OnPaint,
    the other in a different method.


    Just follow this concept:

    Put all your drawing stuff in your dedictaed method, which receive the paint arg,
    like:

    Code:
    private void UpdateGraph(PaintEventArgs e, int aData)
    {
     // draw your stuff only here!
    }
    and then just put this method into the OnPaint:
    Code:
    protected override void OnPain(PaintEventArgs e)
    {
      UpdateGraph(e, aData);
    }

    If you like to refresh the painting by your own,
    just call Refresh(); anywhere in your code (not in OnPaint --> makes recursive calls).


    And you are done!

  3. #3
    Join Date
    Aug 2008
    Posts
    78

    Re: Problem with CreateGraphics()

    Hi,
    Thanks MNovy.thing is I do not want to refresh the whole form because if I do it, I will lose the points that have already been drawn on the graph. In fact, I will be redrawing axis again too.

    If I put in a sentence, purpose is to retain what has already been drawn on the form and just add some new point or line somewhere on the form.

    Any idea about it?

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Problem with CreateGraphics()

    you have to redraw - there is no way out to keep form content alive.
    If you move your windows, it refreshs,
    if you resize your window, it refreshs,
    if you move your cursor or another win over your form, it refreshs...


    To be honest: I do not understand your problem.
    I had a fully 3D-looking dash board and a running graph at the bottom of the window.

    I had never refreshing problems, cause I chose the way I told you above.

  5. #5
    Join Date
    Aug 2008
    Posts
    78

    Re: Problem with CreateGraphics()

    you are right. It refreshes at every action. Consider this example:

    - you are drawing a point on a graph and this point you receivie regularly in every 1 second
    - You draw first point
    - you draw second point
    - you minimize the window and again restore it
    - now by this time you receive third point and draw it but now you can see only third point.

    How can I restore all the points on the form?

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Problem with CreateGraphics()

    Quote Originally Posted by ricky_cpp View Post
    you are right. It refreshes at every action. Consider this example:

    - you are drawing a point on a graph and this point you receivie regularly in every 1 second
    - You draw first point
    - you draw second point
    - you minimize the window and again restore it
    - now by this time you receive third point and draw it but now you can see only third point.

    How can I restore all the points on the form?

    This is not a matter of painting, but a matter of your data organization. You need a data buffer.

    You have to store your data into an array for example, which is as bigger as the X-axis can
    hold data. Once you reach the last element of your array, skip back to the beginning.

    You can then draw all point by using the DrawLines method at once.

  7. #7
    Join Date
    Aug 2008
    Posts
    78

    Re: Problem with CreateGraphics()

    Hi,

    thanks. thats a good idea :-)

    but do you think if I paint my graph on an image and restore+save it with every point that I receive and then load it in onpaint method, it should work. otherwise I will have to store like 10,000 or so points (in 2 mins of time) and repaint whole graph everytime too.

  8. #8
    Join Date
    Mar 2009
    Posts
    20

    Re: Problem with CreateGraphics()

    hey jus make a temp buffer image and save your data on it and repaint it again

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with CreateGraphics()

    Quote Originally Posted by ricky_cpp View Post
    Hi,

    thanks. thats a good idea :-)

    but do you think if I paint my graph on an image and restore+save it with every point that I receive and then load it in onpaint method, it should work. otherwise I will have to store like 10,000 or so points (in 2 mins of time) and repaint whole graph everytime too.
    This is just a lot of unneded effort as in this case you have to read and repaint your picture and you have to store the full bitmap again and again.Instead of simple storing some data like x,y (position ) and color of the dot. and having a simple painting routine that draws the whole thing in every cycle.
    BTW if your graph containd 10.000 items in two minutes then your full graph So what is more effective Drawing in both cases takes place. The additional advantage of storing the data is you are able to evaluate them or to have a screen which is able to move along timeline back and forward oly showing the wanted sector of your statistics curve
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Aug 2008
    Posts
    78

    [RESOLVED] Problem with CreateGraphics()

    Thank you all for valuable inputs.

    I will be using both the solutions suggested.

    I am now using a bmp variable and updating everytime on receival of a point but you said it right that I will need to analyze these points too which means I shall store these points too (data management) so that, I can build features like zoom in and out.

    Am storing all points but just for log and not for painting but hopefully incorporate it next version.

  11. #11
    Join Date
    Aug 2008
    Posts
    78

    Resolved [RESOLVED] Problem with CreateGraphics()

    Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured