Using OxyPlot to render png images from PlotModel

From the documentation on the OxyPlot Documentation Website it says that it uses a class PngExporter. This class no longer exists in OxyPlot, but there are classes called PngEncoderand PngDecoder. I suspect that the equivalent method PngExporter.Exportis equal PngEncoder.Encode, but it requests a 2d array from OxyColor, called "pixels", but I don't know where to get this data. NOTE: Exporting to SVG or PDF works, but this form is useless.

Problem: I need to export PNG from code only PlotModelto OxyPlot, but the documentation is out of date.

This is the code I was told to use:

 using (var stream = File.Create(fileName))
    {
        var pngExporter = new PngExporter();
        pngExporter.Export(plotModel, stream, 600, 400, Brushes.White);
    }
+4
source share
2 answers

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

+2
source

To add a Jamie answer, if you want to export png from 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();

The latest pre-release v1.0.0-unstable2100 is used.

+2
source

Source: https://habr.com/ru/post/1618008/


All Articles