【发布时间】:2016-03-22 22:19:12
【问题描述】:
我正在我的代码中生成一个图表,并将其显示在屏幕上,它看起来不错:带有 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();
}
}
有什么想法吗?
顺便说一句,我使用的技术对我来说是新的;我在薄冰上很遥远。我试过谷歌搜索,但我是空的。我当然会感谢任何帮助。谢谢!
【问题讨论】:
标签: c# wpf charts save data-visualization