<Grid Background="LightGray" >
<chart:SfChart x:Name="chart" AreaBackground="Cyan"
Header="Chart Area Header"
AreaBorderBrush="Red" AreaBorderThickness="5"
Margin="0,0,10,0">
..
</chart:SfChart>
</Grid> |
<Grid Name="mainGrid">
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid x:Name="grid" Background="White">
<chart:SfChart Margin="20" Name="chart" Background="Transparent">
<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis/>
</chart:SfChart.PrimaryAxis>
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis Interval="10"/>
</chart:SfChart.SecondaryAxis>
<chart:ColumnSeries ItemsSource="{Binding Collection}"
XBindingPath="XValue" YBindingPath="YValue"/>
</chart:SfChart>
</Grid>
<Button Height="75" Content="Save Chart" Width="150" Click="Button_Click"
Grid.Row="1"/>
</Grid> |
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void Save( FrameworkElement grid ,string fileName)
{
FrameworkElement element = grid;
string imageExtension = null;
imageExtension = new FileInfo(fileName).Extension.ToLower(System.Globalization.CultureInfo.InvariantCulture);
BitmapEncoder imgEncoder = null;
switch (imageExtension)
{
case ".bmp":
imgEncoder = new BmpBitmapEncoder();
break;
case ".jpg":
case ".jpeg":
imgEncoder = new JpegBitmapEncoder();
break;
case ".png":
imgEncoder = new PngBitmapEncoder();
break;
case ".gif":
imgEncoder = new GifBitmapEncoder();
break;
case ".tif":
case ".tiff":
imgEncoder = new TiffBitmapEncoder();
break;
case ".wdp":
imgEncoder = new WmpBitmapEncoder();
break;
default:
imgEncoder = new BmpBitmapEncoder();
break;
}
if (element != null)
{
RenderTargetBitmap bmpSource = new
RenderTargetBitmap((int)element.ActualWidth,
(int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bmpSource.Render(element);
imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
using (Stream stream = File.Create(fileName))
{
imgEncoder.Save(stream);
stream.Close();
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Save(grid, "a.png");
}
} |