#117 Save Image Error - Parameter is Not valid

open
nobody
5
2013-11-07
2010-05-06
Anonymous
No

Hey All!
Firstly thanks for all the great work, I love ZedGraph!

I save an image and occasionally it flags a weird error: "Parameter Is Not Valid" which occurs on the command below:
Code:
zedChartPrimary.MasterPane.GetImage().Save(sPlayAllDirectory + @"\" + sNextModel + ".png", System.Drawing.Imaging.ImageFormat.Png);

It works fine for most of the time, and for some reason flags the error?

I searched google, there isnt much information on this with regards to ZedGraph.

I switched it to this:
Code:
zedChartPrimary.GraphPane.GetImage().Save(sPlayAllDirectory + @"\" + sNextModel + ".png", System.Drawing.Imaging.ImageFormat.Png);

And it seems to work, but I only get one of the GraphPanes on the master.

Additionally: GetMetaFile() flagged a similar error "Generic GDI+ Error"

Which I searched for and found this:
http://blog.lavablast.com/post/2007/11/The-Mysterious-Parameter-Is-Not-Valid-Exception.aspx and
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/3e065331-d269-40db-b672-d92f55472a84

Which indicates the GDI count (??) is too high, my plots work fine 95% of the time, (they plot one after another) - and between each replot they are reinitialised with a Clear() which is what this forum suggested was the issue (not ZedGraph, but same error)

http://www.telerik.com/community/forums/aspnet/chart/33013-parameter-is-not-valid.aspx
Code:
zedChart.MasterPane.PaneList.Clear();

Tricky problem! Appreciate any ideas

Thanks

Jared

Discussion

  • Tamilvanan
    Tamilvanan
    2013-11-07

    The problem is different friend.

    It's actually because of the invalidate() function that is called everytime after updating the graph.

    Just run validate() function before getimage() or save() funtion and the problem won't occur.

    Ex.

    In my program the zedgraph control is named, graph_live.

    Everytime I update the graph with new plots, I run the following two lines to ensure it updates on screen.

    graph_live.AxisChange();
    graph_live.Invalidate();

    So, when I'm saving an image, It is necessary to run the below function before saving it,

    graph_live.Validate(); // Ignoring this line of code causes the GDI+ error

    try
    {

    using (Bitmap newlog = new Bitmap(graph_live.GraphPane.GetImage()))
    {
    newlog.Save("C:/img08.png", System.Drawing.Imaging.ImageFormat.Png);
    }

    }

    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }