C#在WinForms中将实时图表保存到PNG

C#在WinForms中将实时图表保存到PNG,c#,winforms,bitmap,livecharts,C#,Winforms,Bitmap,Livecharts,我在将LiveCharts PieChart导出到.png文件时遇到困难 到目前为止,我所做的是尝试将控件绘制到位图(),但它只是输出一个黑色图像。我已经放弃了其他选择,例如屏幕截图,因为创建图表不是为了以自定义形式部署以进行可视化。它的主要目的就是图形统计输出 这是我的主要代码: LiveCharts.WinForms.PieChart chart = initializePieChart2DFolder(true); Bitmap bmp = new Bitmap(chart.Width,

我在将LiveCharts PieChart导出到.png文件时遇到困难

到目前为止,我所做的是尝试将控件绘制到位图(),但它只是输出一个黑色图像。我已经放弃了其他选择,例如屏幕截图,因为创建图表不是为了以自定义形式部署以进行可视化。它的主要目的就是图形统计输出

这是我的主要代码:

LiveCharts.WinForms.PieChart chart = initializePieChart2DFolder(true);
Bitmap bmp = new Bitmap(chart.Width, chart.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
chart.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save("graphFolder.png", System.Drawing.Imaging.ImageFormat.Png);
这是我用来创建饼图的方法:

private LiveCharts.WinForms.PieChart initializePieChart2DFolder()
{
     LiveCharts.WinForms.PieChart chart = new LiveCharts.WinForms.PieChart();
     chart.Anchor = System.Windows.Forms.AnchorStyles.None;
     chart.Location = new System.Drawing.Point(17, 56);
     chart.Name = "pieChart2DFolder";
     chart.Size = new System.Drawing.Size(364, 250);
     chart.TabIndex = 0;
     chart.BackColorTransparent = false;
     chart.BackColor = Color.White;
     chart.ForeColor = Color.Black;
     SeriesCollection chartData = new SeriesCollection();
     foreach(var annot in numAnnotsPerLabel)
     {
          System.Windows.Media.Color newColor = System.Windows.Media.Color.FromArgb(color[annot.Key].A, color[annot.Key].R, color[annot.Key].G, color[annot.Key].B);
          chartData.Add( new PieSeries { Title = annot.Key,
                                         Values = new ChartValues<int> { annot.Value },
                                         DataLabels = true,
                                         Stroke = System.Windows.Media.Brushes.DimGray,
                                         Foreground = System.Windows.Media.Brushes.Black,
                                         FontSize = 9,
                                         Fill = new 
         System.Windows.Media.SolidColorBrush(newColor)});
     }
     chart.Series = chartData;
     DefaultLegend customLegend = new DefaultLegend();
     customLegend.BulletSize = 15;
     customLegend.Foreground = System.Windows.Media.Brushes.Black;
     customLegend.Orientation = System.Windows.Controls.Orientation.Vertical;
     customLegend.FontSize = 10;
     chart.DefaultLegend = customLegend;
     chart.LegendLocation = LegendLocation.Right;
     var tooltip = chart.DataTooltip as DefaultTooltip;
     tooltip.SelectionMode = LiveCharts.TooltipSelectionMode.OnlySender;
     return chart;
}
private LiveCharts.WinForms.PieChart初始化Epiechart2dFolder()
{
LiveCharts.WinForms.PieChart chart=新的LiveCharts.WinForms.PieChart();
chart.Anchor=System.Windows.Forms.AnchorStyles.None;
图表位置=新系统图纸点(17,56);
chart.Name=“pieChart2DFolder”;
图表尺寸=新系统图纸尺寸(364250);
chart.TabIndex=0;
chart.BackColorTransparent=false;
chart.BackColor=Color.White;
chart.ForeColor=颜色.黑色;
SeriesCollection chartData=新的SeriesCollection();
foreach(numAnnotsPerLabel中的变量annot)
{
System.Windows.Media.Color newColor=System.Windows.Media.Color.FromArgb(颜色[annot.Key].A,颜色[annot.Key].R,颜色[annot.Key].G,颜色[annot.Key].B);
添加(新系列{Title=annot.Key,
值=新图表值{annot.Value},
DataLabels=true,
笔划=System.Windows.Media.Brusks.DimGray,
前台=System.Windows.Media.brusks.Black,
FontSize=9,
填充=新
System.Windows.Media.SolidColorBrush(newColor)});
}
图表系列=图表数据;
DefaultLegend customLegend=新的DefaultLegend();
customLegend.BulletSize=15;
customLegend.Foreground=System.Windows.Media.brusks.Black;
customLegend.Orientation=System.Windows.Controls.Orientation.Vertical;
customLegend.FontSize=10;
chart.DefaultLegend=自定义图例;
chart.LegendLocation=LegendLocation.Right;
var tooltip=chart.DataTooltip作为默认工具提示;
tooltip.SelectionMode=LiveCharts.TooltipSelectionMode.OnlySender;
收益表;
}

提前非常感谢

可能在制作位图之前必须强制图表渲染一次(也就是说,
DrawToBitmap
可能实际上不是在绘制,只是从用于双缓冲的内部位图复制),特别是,我建议尝试在
DrawToBitmap()之前调用
chart.Refresh()
@BenVoigt是的,我已经尝试过了,但没有任何改变:(请参阅此处的最后一篇文章:,它使用了GitHub repo中所示的相同方法,您可以在其中找到许多其他示例:。这个WinForms图表库显然是一个WPF包装器,因此
RenderTargetBitmap()
是一种更自然的渲染内容的方法。您似乎还需要暂停任何动画。在渲染图像之前。@Jimi非常感谢!这是正确的解决方案。请注意,保存时必须使用LiveCharts.Wpf而不是LiveCharts.WinForms。