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

戏子无情 提交于 2019-12-18 04:12:33

问题


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.


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/265062/load-image-from-file-and-print-it-using-wpf-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!