无法在保存的图表中显示标签和标题(C#.NET,System.Windows.Controls.DataVisualization.Charting)
Can't get labels and title to show up in saved chart (C#.NET, System.Windows.Controls.DataVisualization.Charting)
我正在用我的代码生成一个图表,并将其显示在屏幕上,它看起来不错:带有 X 轴和 Y 轴标签的漂亮曲线。
但是当我去保存为图片时,标签和标题都被涂黑了。
这是图表的 xaml...
<chartingToolkit:Chart Name="chart" Title="Test Results" VerticalAlignment="Top" Height="450" Width="450">
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" Title="" Foreground="Black" Background="Blue" BorderBrush="Blue">
<chartingToolkit:LineSeries.DataPointStyle>
<Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
<Setter Property="Width" Value="2"/>
<Setter Property="Height" Value="2"/>
</Style>
</chartingToolkit:LineSeries.DataPointStyle>
</chartingToolkit:LineSeries>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0"/>
<Setter Property="Height" Value="0"/>
</Style>
</chartingToolkit:Chart.LegendStyle>
</chartingToolkit:Chart>
以及图片保存方法:
private void buttonSaveImage_Click(object sender, RoutedEventArgs e)
{
double printScale = 96; // pixels per inch
// RenderTargetBitmap/Render renders the GUI element in the context of the parent control.
// The area of the parent control that is not the desired element is masked in black pixels.
// We render the chart inside the area of the parent, then crop the area of the chart from the larger image.
renderBitmap = new RenderTargetBitmap((int)chart.ActualWidth, (int)chart.ActualHeight, printScale, printScale, PixelFormats.Default);
// store the rendered chart to the bitmap
renderBitmap.Render(chart);
BitmapEncoder encoder = new JpegBitmapEncoder();
string filename = String.Empty;
// pop up save file dialog, get file name & encoder type (jpg, bmp, png, etc.)
if (TryGetGraphicFilePath(ref filename, ref encoder))
{
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
FileStream fs = new FileStream(filename, FileMode.Create);
encoder.Save(fs);
fs.Flush();
fs.Close();
}
}
有什么想法吗?
顺便说一句,我使用的技术对我来说是新的;我在薄冰上很远。我试过谷歌搜索,但我一无所获。我当然会很感激任何帮助。谢谢!
尝试将图表元素包裹在具有白色背景的网格中,然后在 buttonSaveImage_Click 中尝试呈现网格而不是图表。
希望有用
我正在用我的代码生成一个图表,并将其显示在屏幕上,它看起来不错:带有 X 轴和 Y 轴标签的漂亮曲线。
但是当我去保存为图片时,标签和标题都被涂黑了。
这是图表的 xaml...
<chartingToolkit:Chart Name="chart" Title="Test Results" VerticalAlignment="Top" Height="450" Width="450">
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" Title="" Foreground="Black" Background="Blue" BorderBrush="Blue">
<chartingToolkit:LineSeries.DataPointStyle>
<Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
<Setter Property="Width" Value="2"/>
<Setter Property="Height" Value="2"/>
</Style>
</chartingToolkit:LineSeries.DataPointStyle>
</chartingToolkit:LineSeries>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0"/>
<Setter Property="Height" Value="0"/>
</Style>
</chartingToolkit:Chart.LegendStyle>
</chartingToolkit:Chart>
以及图片保存方法:
private void buttonSaveImage_Click(object sender, RoutedEventArgs e)
{
double printScale = 96; // pixels per inch
// RenderTargetBitmap/Render renders the GUI element in the context of the parent control.
// The area of the parent control that is not the desired element is masked in black pixels.
// We render the chart inside the area of the parent, then crop the area of the chart from the larger image.
renderBitmap = new RenderTargetBitmap((int)chart.ActualWidth, (int)chart.ActualHeight, printScale, printScale, PixelFormats.Default);
// store the rendered chart to the bitmap
renderBitmap.Render(chart);
BitmapEncoder encoder = new JpegBitmapEncoder();
string filename = String.Empty;
// pop up save file dialog, get file name & encoder type (jpg, bmp, png, etc.)
if (TryGetGraphicFilePath(ref filename, ref encoder))
{
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
FileStream fs = new FileStream(filename, FileMode.Create);
encoder.Save(fs);
fs.Flush();
fs.Close();
}
}
有什么想法吗?
顺便说一句,我使用的技术对我来说是新的;我在薄冰上很远。我试过谷歌搜索,但我一无所获。我当然会很感激任何帮助。谢谢!
尝试将图表元素包裹在具有白色背景的网格中,然后在 buttonSaveImage_Click 中尝试呈现网格而不是图表。
希望有用