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

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

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

Tags:

Wpf

Printing