printing visual or what?

Aug 17, 2010 at 11:13 AM

Hi,

i'am trying to print my chart.

double width = printDialog.PrintableAreaWidth; 
double height = printDialog.PrintableAreaHeight; 
Size size = new Size( width - 100, height - 100); 
Point point = new Point((width - size.Width) * 0.5, (height - size.Height) * 0.5); 
_plotter.Measure(size); 
_plotter.Arrange(new Rect(point, size)); 
printDialog.PrintVisual(_plotter, "chart");

this does not work. the grid and 'Graph Lines' are not scaled right.

so i need an example or en explonation how to do it right.

thx.

 

ps: this doesn't help: http://dynamicdatadisplay.codeplex.com/Thread/View.aspx?ThreadId=62813

 

 

Aug 17, 2010 at 11:54 AM

I use this function to print D3 charts:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
using System.Printing;

namespace Utilities
{
    static public class PrintHelper
    {
        static public void Print(FrameworkElement ele, string name)
        {
            //Most printers don't do borderless printing, so this artifically adds a border.
            const double margin = 30;
            const double titlePadding = 10;

            PrintDialog printDlg = new PrintDialog();
            printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
            if (printDlg.ShowDialog() != true) return;

            //Format the title line
            FormattedText formattedText = new FormattedText(name, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 12, Brushes.Black);
            formattedText.MaxTextWidth = printDlg.PrintableAreaWidth;

            double scale = Math.Min(printDlg.PrintableAreaWidth / (ele.ActualWidth + (margin * 2)), (printDlg.PrintableAreaHeight - (formattedText.Height + titlePadding)) / (ele.ActualHeight + (margin * 2)));

            DrawingVisual visual = new DrawingVisual();
            using (DrawingContext context = visual.RenderOpen())
            {
                VisualBrush brush = new VisualBrush(ele);
                context.DrawRectangle(brush, null, new Rect(new Point(margin, margin + formattedText.Height + titlePadding), new Size(ele.ActualWidth, ele.ActualHeight)));

                context.DrawText(formattedText, new Point(margin, margin));
                context.DrawLine(new Pen(Brushes.Black, 1), new Point(margin, margin), new Point(printDlg.PrintableAreaWidth - (2 * margin), margin));
            }

            visual.Transform = new ScaleTransform(scale, scale);

            printDlg.PrintVisual(visual, name);
        }

    }
}

Grant.

Jan 24, 2012 at 2:07 PM

Grant, Thanks so much for that chunk of code, it saved me a ton of time! I just added the following XAML

        <Button Content="Print" Height="24" HorizontalAlignment="Left" Margin="245,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="60" Click="bPrint_Click" />

a little C#

         private void bPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintHelper.Print(grid, chartTitle.ToString());
        }

and I was printing charts! Now for a cup of coffee... :-)

Oct 17, 2013 at 8:14 PM
ydab_lj, grantz and mtimperley,

Thanks so much for asking and the code. It saved me a lot of time, too. Ichecked it out, tried it and the printed charts were wonderful!

Is there any way to give You some benfits, likes or points or something else?

Ottilie