I'm writing an application that has need of making small bitmaps of charts for static preview purposes. I've tried everything I can think of to get RenderTargetBitmap to work with XamChart, but all I get is an empty bitmap (the bitmap is the correct size, it's just filled with transparent pixels). The following code demonstrates the use of RenderTargetBitmap with a regular button and a XamChart. Both are created at run time and are never displayed. Creating a bitmap of the button works, but not XamChart. Is this a bug, or is there a workaround?
<Window x:Class="XamChartTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="XamChartTest" Height="300" Width="300" > <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Button Content="XamChart Test" Click="OnClickChart" Grid.Row="0"/> <Button Content="Regular Test" Click="OnClickTest" Grid.Row="1" /> </Grid></Window>
public void OnClickChart(object sender, RoutedEventArgs e) { XamChart testChart = new XamChart(); testChart.Width = 200; testChart.Height = 200; testChart.Measure(new Size(200, 200)); testChart.Arrange(new Rect(0, 0, 200, 200)); RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32); bmp.Render(testChart); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(@"C:\TestChart.png")) png.Save(stm); } public void OnClickTest(object sender, RoutedEventArgs e) { Button testButton = new Button(); testButton.Content = "Test Button"; testButton.Width = 200; testButton.Height = 200; testButton.Measure(new Size(200, 200)); testButton.Arrange(new Rect(0, 0, 200, 200)); RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32); bmp.Render(testButton); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(@"C:\TestButton.png")) png.Save(stm); }
Hi,
Please, add the chart to the visual tree:
<Window x:Class="WpfApplication12.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igCA="http://infragistics.com/Chart" Title="Window1" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <igCA:XamChart Name="testChart" Margin="0,0,0,0" Width="200" Height="200"/> <Button Content="XamChart Test" Click="OnClickChart" Grid.Row="0"/> <Button Content="Regular Test" Click="OnClickTest" Grid.Row="1" /> </Grid></Window>
public void OnClickChart(object sender, RoutedEventArgs e) { testChart.Measure(new Size(200, 200)); testChart.Arrange(new Rect(0, 0, 200, 200));
RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32); bmp.Render(testChart); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(@"C:\TestChart.png")) png.Save(stm); }
Thanks,GoranS
I think perhaps I wasn't 100% clear. I don't want to display the chart when I do this, I want to make a thumbnail of the chart, keeping that chart 'off screen'. The XamChart object itself is not displayed to the user at this point, and we don't want it to be a part of the visual tree.
So, how can I get RenderTargetBitmap to work without adding the chart to the visual tree (like I'm generating the button bitmap)?