Load image from file and print it using WPF... how?

19,031

Solution 1

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");
}

Solution 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

Solution 3

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.

Solution 4

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");
}
Share:
19,031
Jon Kruger
Author by

Jon Kruger

Project Engineer with Quick Solutions in Columbus, OH

Updated on July 27, 2022

Comments

  • Jon Kruger
    Jon Kruger almost 2 years

    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.

  • Uthistran Selvaraj.
    Uthistran Selvaraj. almost 11 years
    Tamir ... its fine.... In case I need to print multiple images..., how can I achieve ?
  • AgentFire
    AgentFire over 4 years
    that doesn't always produce very qualitative outputs.
  • agileDev
    agileDev about 4 years
    Truncating my image length. Also height is almost same, but width is calculating wrong. But print driver is populated well. Any suggestion?