Jamie Mair
Jamie Mair

Reputation: 336

Using OxyPlot to render png images from a PlotModel

From the documentation on the OxyPlot Documentation Website, it says to use the PngExporter class. This class no longer exists in OxyPlot, but rather there are classes called PngEncoder and PngDecoder. I suspect the equivalent method to PngExporter.Export is PngEncoder.Encode however it asks for a 2d array of OxyColor called "pixels", but I do not know where to get this data from. NOTE: Exporting to SVG or PDF works but this form is useless.

Problem: I need to export PNGs from code only of a PlotModel in OxyPlot but documentation is outdated.

This is the code I have been told to use:

 using (var stream = File.Create(fileName))
    {
        var pngExporter = new PngExporter();
        pngExporter.Export(plotModel, stream, 600, 400, Brushes.White);
    }

Upvotes: 4

Views: 4363

Answers (2)

James Blake
James Blake

Reputation: 1118

To add to Jamie's answer, if you want to export a png from say a class library, you can do something like this with STAThread:

        var thread = new Thread(() =>
        {
            PngExporter.Export(plotModel, @"C:\file.png", 600, 400, OxyColors.White);
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();

This is using the latest pre-release v1.0.0-unstable2100.

Upvotes: 4

Jamie Mair
Jamie Mair

Reputation: 336

Using the [GitHub Oxyplot] files to build the librarys, both oxyplot and oxyplot.wpf , and then use these libraries instead. Note any method which exports a PNG must be have the STAThread tag.

Upvotes: 1

Related Questions