Generating and Saving ZedGraph plots without showing on forms
Asked Answered
A

2

10

Is it possible to plot data on to a ZedGraph graph and save it as a file without showing / generating a graph that is visible to the user? I'm looking to process a lot of datasets and generate a graph and saving it to a file for viewing outside of the application.

If this can't be done, would it be possible show the graph on a hidden/minimized form, save the graph, close the window, and repeat for each graph?

Aleron answered 7/7, 2011 at 17:41 Comment(0)
A
7

It is possible.

You create and manipulate the ZedGraph control as usual, but just don't add it to the Form.Controls list, for example, in the InitializeComponent() method, comment out something that looks like the below

this.Controls.Add(this.zedGraphControl);

There are a couple of ways to save the graph

  • If you want a SaveAs dialog to appear, call SaveAs() on the graph control.
  • If you don't want the dialog, you can write out the image using GetImage() on the MasterPane, and then save that:

    zedGraphControl.MasterPane.GetImage().Save("test.bmp");

Amado answered 8/7, 2011 at 12:29 Comment(3)
I have a BackgroundWorker that runs the code to generate a graph, as well as other tasks, asynchronously. I've determined that generating a ZedGraphControl on a background thread will cause the thread to not Complete (via TaskComplete event), even though the DoWork method executes. Is there any other method to do this?Aleron
I just tried this and it worked for me, I registered with worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted);Amado
Sorry, I actually have an event that is fired sometime after a backgroundworker completes. The event handler generates the graph, and then kicks off that same BackgroundWorker again. The second time the BGWorker runs, it never gets to TaskComplete. I had to put the graph generation in a seperate BGWorker for some reason. It's quite strange.Aleron
C
5

Here is a code snippet to create and save the Bitmaps without any WinForms infrastructure necessary:

var zedGraph = new ZedGraphControl();

// configure ZedGraphControl here

using (var g = zedGraph.CreateGraphics())
{
    zedGraph.MasterPane.ReSize(g, new RectangleF(0, 0, widthPx, heightPx));
}
zedGraph.MasterPane.GetImage().Save(Path.Combine(destinationDir, "test.bmp"));

This should even be able to run as service without any desktop. The only downside is that you need to reference System.Windows.Forms and System.Drawing to use it.

Cindicindie answered 12/3, 2012 at 16:22 Comment(2)
This must be an old version of ZedGraph. MasterPane is not defined here. GraphPane is, but it does not have the GetImage() method. Can you update this code, please?Noseband
This answer is from 2012. I haven't touched ZedGraph since 2013.Cindicindie

© 2022 - 2024 — McMap. All rights reserved.