使用 OxyPlot 从 PlotModel 渲染 png 图像
Using OxyPlot to render png images from a PlotModel
OxyPlot Documentation Website 的文档中说要使用 PngExporter
class。这个 class 不再存在于 OxyPlot 中,而是有 class 称为 PngEncoder
和 PngDecoder
。我怀疑 PngExporter.Export
的等效方法是 PngEncoder.Encode
,但是它要求一个名为 "pixels" 的 OxyColor
的二维数组,但我不知道从哪里获取这些数据。
注意:导出为 SVG 或 PDF 有效,但此表单无用。
问题: 我只需要从 OxyPlot 中的 PlotModel
代码导出 PNG,但文档已过时。
这是我被告知要使用的代码:
using (var stream = File.Create(fileName))
{
var pngExporter = new PngExporter();
pngExporter.Export(plotModel, stream, 600, 400, Brushes.White);
}
使用 [GitHub Oxyplot] 文件构建 oxyplot 和 oxyplot.wpf 库,然后改用这些库。
请注意,任何导出 PNG 的方法都必须具有 STAThread 标记。
添加到 Jamie 的回答中,如果你想从 class 库中导出 png,你可以使用 STAThread 做这样的事情:
var thread = new Thread(() =>
{
PngExporter.Export(plotModel, @"C:\file.png", 600, 400, OxyColors.White);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
这是使用最新的预发布版本 v1.0.0-unstable2100。
OxyPlot Documentation Website 的文档中说要使用 PngExporter
class。这个 class 不再存在于 OxyPlot 中,而是有 class 称为 PngEncoder
和 PngDecoder
。我怀疑 PngExporter.Export
的等效方法是 PngEncoder.Encode
,但是它要求一个名为 "pixels" 的 OxyColor
的二维数组,但我不知道从哪里获取这些数据。
注意:导出为 SVG 或 PDF 有效,但此表单无用。
问题: 我只需要从 OxyPlot 中的 PlotModel
代码导出 PNG,但文档已过时。
这是我被告知要使用的代码:
using (var stream = File.Create(fileName))
{
var pngExporter = new PngExporter();
pngExporter.Export(plotModel, stream, 600, 400, Brushes.White);
}
使用 [GitHub Oxyplot] 文件构建 oxyplot 和 oxyplot.wpf 库,然后改用这些库。 请注意,任何导出 PNG 的方法都必须具有 STAThread 标记。
添加到 Jamie 的回答中,如果你想从 class 库中导出 png,你可以使用 STAThread 做这样的事情:
var thread = new Thread(() =>
{
PngExporter.Export(plotModel, @"C:\file.png", 600, 400, OxyColors.White);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
这是使用最新的预发布版本 v1.0.0-unstable2100。