CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Add graph to form

    I have created a line graph in C# but it doesnt appear. Ideally I would like it to displayed on a new Windows Form or in IE. This is the code I used to plot it on stage:

    Code:
     protected void PlotGraph()
            {
                // Plot the whole graph in form at coordinates (0,0)
                Form statsForm = new Form();
                objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                statsForm.Controls.Add(objGraphics.DrawImageUnscaled(objBitmap, new Point(0,0))); // Doesnt work!
           
                // ALSO TRIED: 
                // objGraphics.DrawImageUnscaled(objBitmap, new Point(0, 0)); // DOESNT APPEAR
    
                // AND:
                //objBitmap.Save(Response.OutputStream, ImageFormat.Png); // DOESNT APPEAR
    
                // AND:
                // Render BitMap Stream Back To Client
                //Bitmap StockBitMap;
                //Response.ContentType = "image/png";
                //MemoryStream memStream = new MemoryStream();
                //StockBitMap.Save(memStream, ImageFormat.Png);
                //memStream.WriteTo(Response.OutputStream);
            }

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Add graph to form

    Code:
    Form statsForm = new Form();
    What form is that? It doesn't have any properties (size, position, styles, parent, etc.).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: Add graph to form

    Is that necessary? I thought it would create a form according to the default value. But shouldnt the graph image come up just by using the following method? I ve seen that on many examples.

    Code:
    objGraphics.DrawImageUnscaled(objBitmap, new Point(0, 0));

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

    Re: Add graph to form

    That is a very odd and unreliable way to draw a graph. Any drawing should take place in the OnPaint method. I don;t know where you are getting that graphics object, and you are passing in the result of DrawUnscaled to Form.Controls.Add. That will not work because the add method expects a control.

    Your form does not appear because you never show it; call the Show method.

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

    Re: Add graph to form

    The whole code you show is a bit strange. Its somehow if you want to drive a car without having a car, but something you think it is a car. No your Form cannot be visible as its size is not fixed and it also is never been shown ( Missing ---> statsForm.Show() command ) But it still will not work to add an objectGraphics as a Control !
    The way to do is:
    1) Create a Form derived from class Form.
    2) Set its size and all needed properties
    3) Overide its OnPaint Event and therein create your graphics method which draws the graphic. But you need to exactly define each line and each point to be drawn !

    The other way would be to already have a canvas where you can draw it. This way you would also already have the OnPaint event of that Canvas and you may put your paint method there.

    But maybe as a first step you really should get a good book about C# and studying how classes work, how Forms are created and shown on the screen, how userdefined drawing works and all that things, as IMHO sorry to say, you are missing lots of this basics.

    I ve seen that on many examples....
    Where have you seen such sort of examples I never have seen things like that.
    Last edited by JonnyPoet; February 27th, 2010 at 11:52 AM.
    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

  6. #6
    Join Date
    Jan 2010
    Posts
    130

    Re: Add graph to form

    Thank you, I have followed your advice and have created a new windows form. In the initialization method of the windows form I call up the method to create the graph:
    Code:
    public partial class FormStatistics : Form
        {
            // My variables were here but I choose not to display them
    
            public FormStatistics()
            {
                InitializeComponent();
                InitializeGraph(); // Creates the graph
            }
    To access this new form the user must click a certain button on my original form. A new instance is then created of this form:
    Code:
            private void btnStatisticsGenerate_Click(object sender, EventArgs e)
            {
                FormStatistics stats = new FormStatistics();
                stats.Show();
            }

    My form however stays empty; it doesnt contain the graph image that I programmatically added to the form's pictureBox!
    Code:
    (...)
    protected void OnPaint() // or override?
            {
                // Set graph title in center of stage
                int center = miWidth / 2;
                objGraphics.DrawString(mTitle, mFont, mGraphBrush, new Point(center, miHeight));
    
                // Paint the whole graph in form at coordinates (0,0)
                objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                objGraphics.DrawImageUnscaled(objBitmap, new Point(0, 0)); // leave out?
                objGraphics = this.pbxStatistics.CreateGraphics();
               
                // Alt tag is equal to graph type
                pbxStatistics.Tag = mTitle;
                pbxStatistics.Refresh();
            }

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

    Re: Add graph to form

    If you dont override the existing OnPaint method it will not work !
    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

  8. #8
    Join Date
    Jan 2010
    Posts
    130

    Re: Add graph to form

    I read that when overriding OnPaint in a derived class, one must be sure to call the base class's OnPaint method so that registered delegates receive the event (http://msdn.microsoft.com/en-us/libr...nt(VS.71).aspx). I'm however unsure as to what parameters OnPaint() has.
    I am trying to call it here:
    Code:
    public FormStatistics()
            {
                InitializeComponent();
                InitializeGraph();
            }
    
            protected void InitializeGraph()
            {
                LoadAllData();
                DrawAxis();
                DrawLine();
                OnPaint(); // What parameters?
            }
    Where my new OnPaint method is:
    Code:
    protected override void OnPaint(PaintEventArgs e) //PaintEventArgs contains the event data.
            {
                // Set graph title in center of stage
                int center = miWidth / 2;
                objGraphics.DrawString(mTitle, mFont, mGraphBrush, new Point(center, miHeight));
    
                // Alt tag is equal to graph type
                pbxStatistics.Tag = mTitle;
                pbxStatistics.Refresh();
    
                try
                {
                    // Paint the whole graph in form at coordinates (0,0)
                    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    objGraphics.DrawImageUnscaled(objBitmap, new Point(0, 0)); 
                    objGraphics = this.pbxStatistics.CreateGraphics();
                    // OR  base.OnPaint(e);
                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message);
                }
            }

  9. #9
    Join Date
    Jan 2010
    Posts
    130

    Re: Add graph to form

    I took away the Refresh() and my graph now appears without having to call the OnPaint method! If one did need to call it one could probably use the following code in the constructor:

    Code:
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

  10. #10
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: [RESOLVED] Add graph to form

    Checkout e.Graphics, where 'e' is passed into the OnPaint handler.
    Code:
    Graphics g = e.Graphics;
    This is what is generally the first line of a User written OnPaint override.

    If you call the Invalidate() method on a control, it will cause that control's OnPaint handler to be called. That's how you call OnPaint, not directly.
    Last edited by rliq; March 2nd, 2010 at 11:12 PM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  11. #11
    Join Date
    Jan 2010
    Posts
    130

    Re: [RESOLVED] Add graph to form

    Thanks, ill keep that in mind!

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

    Re: Add graph to form

    Code:
    public FormStatistics()
            {
                InitializeComponent();
                InitializeGraph();
            }
     
            protected void InitializeGraph()
            {
                LoadAllData();
                DrawAxis();
                DrawLine();
                OnPaint(); // What parameters?´NO !! onPaint is called invaidating the form !!
            }
    Where my new OnPaint method is:
    Code:
    protected override void OnPaint(PaintEventArgs e) //PaintEventArgs contains the event data.
            {
                Graphics objGraphics = e.Graphics; // This is missing
                // Set graph title in center of stage
                int center = miWidth / 2;
                objGraphics.DrawString(mTitle, mFont, mGraphBrush, new Point(center, miHeight));
     
                // Alt tag is equal to graph type
                pbxStatistics.Tag = mTitle;
                pbxStatistics.Refresh();
     
                try
                {
                    // Paint the whole graph in form at coordinates (0,0)
                    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    objGraphics.DrawImageUnscaled(objBitmap, new Point(0, 0)); 
                    objGraphics = this.pbxStatistics.CreateGraphics();
                    // OR  base.OnPaint(e);
                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message); // Never a Messageox in an OnPaint method. God No !
                }
            }
    I simple added some comments into your code
    Most of it is very questionable but I dont know what pbxStatistics is
    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

Tags for this Thread

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