8

I'm looking for an example of how to load an image from file and print it on a page using WPF. I'm having a hard time finding good information about WPF printing.

Jon Kruger
  • 103
  • 1
  • 2
  • 7

4 Answers4

26
var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri("");
bi.EndInit();

var vis = new DrawingVisual();
using (var dc = vis.RenderOpen())
{
    dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });
}

var pdialog = new PrintDialog();
if (pdialog.ShowDialog() == true)
{
    pdialog.PrintVisual(vis, "My Image");
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Tamir
  • 2,503
  • 16
  • 23
2

If you want more control then PrintDialog.PrintVisual gives you you have to wrap your image in a FixedDocumet.

You can find simple code that creates a fixed document here: http://www.ericsink.com/wpf3d/B_Printing.html

Nir
  • 29,306
  • 10
  • 67
  • 103
1

did playing around with this.

Tamir's answer is a great answer, but the problem is, that it's using the original size of the image.


write a solution myself , whitch doesn't stretch the image if it's smaller than the pagesize and bring the image if it's to large at the page.
It can be used for multiply copies and can be used with both orientations.

                PrintDialog dlg = new PrintDialog();

            if (dlg.ShowDialog() == true)
            {
                BitmapImage bmi = new BitmapImage(new Uri(strPath));

                Image img = new Image();
                img.Source = bmi;

                if (bmi.PixelWidth < dlg.PrintableAreaWidth ||
                           bmi.PixelHeight < dlg.PrintableAreaHeight)
                {
                    img.Stretch = Stretch.None;
                    img.Width = bmi.PixelWidth;
                    img.Height = bmi.PixelHeight;
                }


                if (dlg.PrintTicket.PageBorderless == PageBorderless.Borderless)
                {
                    img.Margin = new Thickness(0);
                }
                else
                {
                    img.Margin = new Thickness(48);
                }
                img.VerticalAlignment = VerticalAlignment.Top;
                img.HorizontalAlignment = HorizontalAlignment.Left;

                for (int i = 0; i < dlg.PrintTicket.CopyCount; i++)
                {
                    dlg.PrintVisual(img, "Print a Large Image");
                }
            }

It only works for pictures from files with a path at the moment, but with a little bit work you can adept it and passing only a BitmapImage.
And it is useable with borderless prints (if your printer does support it)


Had to go the way with BitmapImage because it loads the default size of the image.
Windows.Controls.Image doesn't shows the right height and width if you're loading the image directly there.


I'm knowing, that the question is very old, but it was very difficult to found some useful information during searching this.
Hopefully my post will help other people.

schorsch1989
  • 55
  • 1
  • 8
0

Just load the image and apply it to a visual. Then use the PrintDialog to do the work.

...
PrintDialog printer = new PrintDialog();

if (printer.ShowDialog()) {
  printer.PrintVisual(myVisual, "A Page Title");
}
MojoFilter
  • 12,256
  • 14
  • 53
  • 61