目前,我有一个视图,可以说它有4张图像。但是我只想打开一次“打印预览”对话框,然后打印所有图像。目前,我可以使用此代码执行此操作。但是在PrintPreview对话框上有一个名为PrintToFile的选项。问题是,它为每个图像带来了“保存文件”对话框,如果有4个图像,则会出现4次。是否可以只打开一次对话框并打印所有4张图像。
public static void PrintChartsWithSinglePrintPreviewDialog(string[] files)
{
if (files == null || files.Count() < 1)
{
return;
}
using (var pdoc = new PrintDocument())
{
using (var pdi = new System.Windows.Forms.PrintDialog { Document = pdoc, UseEXDialog = true })
{
if (pdi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pdoc.PrinterSettings = pdi.PrinterSettings;
pdoc.PrintPage += Pdoc_PrintPage;
foreach (var file in files)
{
pdoc.DocumentName = file;
pdoc.Print();
//pdoc.Dispose();
}
PrinterSettings settings = new PrinterSettings();
Xceed.Wpf.Toolkit.MessageBox.Show(string.Format(System.Windows.Application.Current.TryFindResource("PrintSuccessful").ToString(), settings.PrinterName),
"Print Successful", MessageBoxButton.OK);
}
}
}
private static void Pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
string file = ((PrintDocument)sender).DocumentName;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(file))
{
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
}
}
我看过PrinterSettings类,但是它只有一个bool属性,可以启用还是禁用PrintToFile选项。