This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I am trying to print a chart using some sample code found on this site. Everything prints well but the actual graph lines and I am not sure why. Any ideea what am I doing wrong? Thanks, Peter private void PrintXPSClick(object sender, RoutedEventArgs e) { string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "chart.xps"); Size pageSize = new Size(420, 320); Point printableOrigin = new Point(10, 10); Size printableSize = new Size(400, 300); FixedDocument document = new FixedDocument(); FixedPage page = new FixedPage(); page.Width = pageSize.Width; page.Height = pageSize.Height; // Create a visual brush that copies the visual of the chart so we can add it to a print document. VisualBrush chartBrush = new VisualBrush(Chart) { Stretch = Stretch.Uniform }; chartBrush.ViewboxUnits = BrushMappingMode.Absolute; chartBrush.Viewbox = new Rect(0, 0, Chart.ActualWidth, Chart.ActualHeight); Rectangle diagramPrintTile = new Rectangle { Width = printableSize.Width, Height = printableSize.Height, Fill = chartBrush }; FixedPage.SetLeft(diagramPrintTile, printableOrigin.X); FixedPage.SetTop(diagramPrintTile, printableOrigin.Y); page.Children.Add(diagramPrintTile); PageContent pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(page); document.Pages.Add(pageContent); // Print the document using and Xps document writer Package package = Package.Open(path, System.IO.FileMode.Create, FileAccess.ReadWrite); XpsDocument xpsDocument = new XpsDocument(package); XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument); writer.Write(document); package.Close(); package = Package.Open(path, System.IO.FileMode.Create, FileAccess.ReadWrite); xpsDocument = new XpsDocument(package); writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument); writer.Write(document); package.Close(); System.Diagnostics.Process.Start(@"c:\program files\internet explorer\iexplore.exe", path); } private void Png_Click(object sender, RoutedEventArgs e) { string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "chart.png"); using (Stream stream = File.Create(path)) { int dpi = 96; RenderTargetBitmap rtb = new RenderTargetBitmap((int)Grid.ActualWidth, (int)Grid.ActualHeight, dpi, dpi, PixelFormats.Pbgra32); rtb.Render(Grid); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(stream); } } |
|
|
Hello Peter This code is working fine at my end. Are you printing a chart that has already been loaded in a window?, or are you printing a chart that has only been built in code? Jason Fauchelle |
|
|
Hi Jason, I just spoke to the engineer who is doing this and he is trying to print a chart that has only been build in code. Peter |
|
|
Hello Peter During the initial sizing pass of the chart control, several things can cause the chart to build the data. To improve the performance, we wait until the chart is fully loaded before rendering the data so that the heavy BuildChart method only gets called once. Because of this, printing a chart that has not been loaded is a bit difficult. Here is a modified version of the code you posted that works better for charts built in code:
A few things to notice here: The ForceRender method puts the chart in an invisible Window to cause it to load. The Print method is called through a dispatcher to ensure the Chart control if fully loaded by the Window. Before creating the VisualBrush, the Chart control is manually arranged. At the end of the Print method, the Window is closed. I know this is very kludgy, but it gets the job done. In a future version we hope to improve the chart printing capabilities. Jason Fauchelle |
|
|
Hi Jason, Thanks for your help. I spoke with the engineer whio is implementing this and he is seeing some issues. Here is his reply. "I tried the code you forwarded to me. There are still several issues. First, I had to comment out the 'OnApplyTemplate' method call in the PrintXPS_Click method to prevent a 'object reference not set to an instance of an object' exception. Second, as you will observe below, the x-axis does not print. Third, only one of the bars seems to be printing - it appears to be the bar for "String 3." Thanks for your help." Unfortunately, I am unable to upload any pics to your site. No error reporting but the hourglass keeps turning when I try to attach a png file. Any ideas what we might be doing wrong? Peter |
|
|
Hello Peter Removing the OnApplyTemplate call will be fine. Not sure what will be causing the other 2 issues. You can send the image to jason@mindscape.co.nz. Additionally, a repro project will help too. Jason Fauchelle |
|