[ACCEPTED]-Load image from file and print it using WPF... how?-printing

Accepted answer
Score: 26
var bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri("");

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


Score: 2

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

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

Score: 1

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

PrintDialog printer = new PrintDialog();

if (printer.ShowDialog()) {
  printer.PrintVisual(myVisual, "A Page Title");
Score: 1

did playing around with this.

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

write 18 a solution myself , whitch doesn't stretch 17 the image if it's smaller than the pagesize 16 and bring the image if it's to large at 15 the page.
It can be used for multiply copies 14 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);
                    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 13 only works for pictures from files with 12 a path at the moment, but with a little 11 bit work you can adept it and passing only 10 a BitmapImage.
And it is useable with borderless 9 prints (if your printer does support it)

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

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

More Related questions