none
printing results RRS feed

  • Question

  • Hi, I am creating a WPF App from code I have used in a previous Windows Forms app.  I have the app working as I require (calculations perform OK, etc) but I also wish to enable the user to print the results (or a screenshot of the WPF window). I tried using the following code:

     private void PrintBtn_Click(object sender, RoutedEventArgs e)
            {
                CaptureScreen();
                printDocument1.Print();
            }
            Bitmap memoryImage;
            
            private void CaptureScreen()
            {
                Graphics myGraphics = this.CreateGraphics();
                Size s = this.Size;
                memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
            }
    
            private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintEventArgs e)
            {
                e.Graphics.DrawImage(memoryImage, 0, 0);
            }


    but only get a list of errors (yes I have included System.Drawing and System.Drawing.Printing in the references and using statements). Errors are as follows:

    Error CS1061 'QuadWindow' does not contain a definition for 'CreateGraphics' and no accessible extension method 'CreateGraphics' accepting a first argument of type 'QuadWindow' could be found (are you missing a using directive or an assembly reference?) 

    Error CS0104 'Size' is an ambiguous reference between 'System.Drawing.Size' and 'System.Windows.Size' 

    Error CS1061 'QuadWindow' does not contain a definition for 'Size' and no accessible extension method 'Size' accepting a first argument of type 'QuadWindow' could be found (are you missing a using directive or an assembly reference?) 

    Maybe I am using the wrong commands for printing in WPF as these were what I had used successfully in my Windows Forms app.  Any help here to steer in the right direction would be appreciated.


    Nigel..


    • Edited by m0cvo Saturday, October 12, 2019 8:43 AM
    Saturday, October 12, 2019 8:42 AM

Answers

  • Hi,
    try this code:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    namespace WpfApp1
    {
      /// <summary>
      /// Interaction logic for Window75.xaml
      /// </summary>
      public partial class Window75 : Window
      {
        public Window75()
        {
          InitializeComponent();
        }
    
    
        private void PrintBtn_Click(object sender, RoutedEventArgs e)
        {
          {
            Image img = new Image() { Stretch = Stretch.UniformToFill };
            img.Source = CreateBitmapSource(this);
            PrintDialog printDlg = new PrintDialog();
            if (printDlg.ShowDialog() == true) printDlg.PrintVisual(img, "Print Window");
          }
        }
    
        public BitmapSource CreateBitmapSource(FrameworkElement source, double scale = 1)
        {
          double actualWidth = source.RenderSize.Width; // same as source.actualwidth - rendersize also works with UIElements
          double actualHeight = source.RenderSize.Height; // same as source.actualHeight- rendersize also works with UIElements
          double renderWidth = actualWidth * scale;
          double renderHeight = actualHeight * scale;
          RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Default);
          VisualBrush sourceBrush = new VisualBrush(source);
          DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual();
          DrawingContext drawingContext = drawingVisual.RenderOpen() as DrawingContext;
          using (drawingContext)
          {
            drawingContext.PushTransform(new ScaleTransform(scale, scale));
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
          }
          renderTarget.Render(drawingVisual);
          return renderTarget;
        }
      }
    } 


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Sunday, October 13, 2019 2:47 AM
    • Marked as answer by m0cvo Sunday, October 13, 2019 8:06 AM
    Sunday, October 13, 2019 2:44 AM

All replies

  • Deleted
    Saturday, October 12, 2019 10:07 PM
  • Hi,
    try this code:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    namespace WpfApp1
    {
      /// <summary>
      /// Interaction logic for Window75.xaml
      /// </summary>
      public partial class Window75 : Window
      {
        public Window75()
        {
          InitializeComponent();
        }
    
    
        private void PrintBtn_Click(object sender, RoutedEventArgs e)
        {
          {
            Image img = new Image() { Stretch = Stretch.UniformToFill };
            img.Source = CreateBitmapSource(this);
            PrintDialog printDlg = new PrintDialog();
            if (printDlg.ShowDialog() == true) printDlg.PrintVisual(img, "Print Window");
          }
        }
    
        public BitmapSource CreateBitmapSource(FrameworkElement source, double scale = 1)
        {
          double actualWidth = source.RenderSize.Width; // same as source.actualwidth - rendersize also works with UIElements
          double actualHeight = source.RenderSize.Height; // same as source.actualHeight- rendersize also works with UIElements
          double renderWidth = actualWidth * scale;
          double renderHeight = actualHeight * scale;
          RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Default);
          VisualBrush sourceBrush = new VisualBrush(source);
          DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual();
          DrawingContext drawingContext = drawingVisual.RenderOpen() as DrawingContext;
          using (drawingContext)
          {
            drawingContext.PushTransform(new ScaleTransform(scale, scale));
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
          }
          renderTarget.Render(drawingVisual);
          return renderTarget;
        }
      }
    } 


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Sunday, October 13, 2019 2:47 AM
    • Marked as answer by m0cvo Sunday, October 13, 2019 8:06 AM
    Sunday, October 13, 2019 2:44 AM
  • Hi, 

    Thanks for that it does actually work but I lose the left hand side of the page.  I also needed to add the following using statements:

    using Point = System.Windows.Point;
    using Image = System.Windows.Controls.Image;

    to prevent the ambiguity of Point and image.

    One simple solution I found on stack overflow is:

    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog().GetValueOrDefault(false))
    {
        printDialog.PrintVisual(this, this.Title); 
    }

    which, as I am only printing the contents of the window, does print the full image. Might not be as tidy but does work.


    Nigel..

    Sunday, October 13, 2019 7:59 AM
  • Sorry, that should read "I lose the right hand side of the page"

    Nigel..

    Sunday, October 13, 2019 8:00 AM