I have a requirement to include some images from our XamDataCharts(scatter and column) into seperate RTF reporting part of the application. The problem is that the placeholders for the charts are different sizes than the chart visualization page. I tried a couple of different things to resize the images but nothing looked right. I tried to clone the control using XamlWriter.Save() but it just hung. So I decided to create an in memory copy of the control, bind it up, set it to the desired size, and then call rendertargetbitmap to grab the image. This worked great for the 'generic' controls on the page but the xamdatachart resized but would not show the actual chart data.
public BitmapSource GetBar() { var vm = DataContext as MyChartViewModel; var are = new AutoResetEvent(false); var lc = new myBarChart { DataContext = vm }; lc.Data = vm.BarChartData; lc.BarChartSource = vm.SelectectedBarChartDisplayMode.SourceType; lc.Width = 350; lc.Height = 400; lc.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() => { are.Set(); })); are.WaitOne(2500); lc.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); lc.Arrange(new Rect(lc.DesiredSize)); return lc.Export();//calls rendertargetbitmap }
Hello,
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well. Also I can say that this seems to be the best appraoch for achieveing the functionality you want.
Found my own problem, I was missing an updatelayout after the measure and arrange.
So now it works the question remains if this is the best method to accomplish my goal...