C# 显示一个打印对话框以打印目录中的多个文件或将所有文件发送到打印机

C# 显示一个打印对话框以打印目录中的多个文件或将所有文件发送到打印机,c#,.net,winforms,printing,dialog,C#,.net,Winforms,Printing,Dialog,我正在使用WinForms。我希望能够打印特定目录中的所有TIF图像。我的代码的问题是,当我尝试打印所有tif图像时,会为每个tif图像打开“打印”对话框。例如,如果目录中有10个tif图像,则每个tif图像的“打印”对话框将打开10次 目标:从这两个选项中 有一个对话框,如图2所示,显示所有tif 要打印的图像 不使用打印对话框将目录中的所有文件发送到默认打印机 突然出现 List<string> elements = new List<string>(); priv

我正在使用
WinForms
。我希望能够打印特定目录中的所有TIF图像。我的代码的问题是,当我尝试打印所有tif图像时,会为每个tif图像打开“打印”对话框。例如,如果目录中有10个tif图像,则每个tif图像的“打印”对话框将打开10次

目标:从这两个选项中

  • 有一个对话框,如图2所示,显示所有tif 要打印的图像
  • 不使用打印对话框将目录中的所有文件发送到默认打印机 突然出现

    List<string> elements = new List<string>();
    private int ElementCounter;
    private void btn_Print_Click(object sender, EventArgs e)
    {
        DirectoryInfo directory = new DirectoryInfo(@"C:\image\Shared_Directory\Printing_Folder\");
        FileInfo[] Files = directory.GetFiles("*.tif"); //Getting Tif files
    
    
        foreach (FileInfo file in Files)
        {               
    
            elements.Add(file.Name);
            string FileToPrint = directory + elements[ElementCounter];
    
            //Print
            ProcessStartInfo info = new ProcessStartInfo(FileToPrint);
            info.Verb = "Print";
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(info);
    
            ElementCounter++;
        }
    }
    
    List elements=newlist();
    专用内部元素计数器;
    私有无效btn\u打印\u单击(对象发送者,事件参数e)
    {
    DirectoryInfo directory=newdirectoryinfo(@“C:\image\Shared\u directory\Printing\u Folder\”);
    FileInfo[]Files=directory.GetFiles(*.tif”);//获取tif文件
    foreach(文件中的文件信息文件)
    {               
    elements.Add(file.Name);
    字符串FileToPrint=directory+elements[ElementCounter];
    //印刷品
    ProcessStartInfo info=新的ProcessStartInfo(FileToPrint);
    info.Verb=“打印”;
    info.CreateNoWindow=true;
    info.WindowStyle=ProcessWindowStyle.Hidden;
    进程启动(信息);
    ElementCounter++;
    }
    }
    
    问题

图2:此打印对话框包含所有要打印的tif图像。我在这里使用WindowsExplore来展示一个示例。

您可以将PrintDocument对象与PrintPreviewDialog一起使用:

private PrintDocument printDoc;
private PrintPreviewDialog printPreview;
List<string> elements = new List<string>();
private int ElementCounter;
private int page;

printDoc.BeginPrint += PrintDoc_BeginPrint;
printDoc.PrintPage += PrintDoc_PrintPage;

private void PrintDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    page = 0;
}

private void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(Image.FromFile(elements[page]), e.MarginBounds);
    page++;
    e.HasMorePages = page < elements.Count;
}

private void btn_Print_Click(object sender, EventArgs e)
{
    DirectoryInfo directory = new DirectoryInfo(@"C:\image\Shared_Directory\Printing_Folder\");
    FileInfo[] Files = directory.GetFiles("*.tif"); //Getting Tif files


    foreach (FileInfo file in Files)
    {              
        elements.Add(file.FullName);
        ElementCounter++;
    }

    printPreview.Document = printDoc;
    printPreview.Show();
}
私有打印文档printDoc;
私有打印预览对话框打印预览;
列表元素=新列表();
专用内部元素计数器;
私人网页;
printDoc.BeginPrint+=printDoc_BeginPrint;
printDoc.PrintPage+=printDoc\u PrintPage;
私有void PrintDoc_BeginPrint(对象发送方,System.Drawing.Printing.PrintEventArgs e)
{
page=0;
}
私有void PrintDoc_PrintPage(对象发送者,System.Drawing.Printing.PrintPageEventArgs e)
{
e、 Graphics.DrawImage(Image.FromFile(elements[page]),e.MarginBounds);
page++;
e、 HasMorePages=page
hi感谢您的回复,但是当我将代码粘贴到
printDoc.BeginPrint+=printDoc\u BeginPrint
printDoc.PrintPage+=printDoc\u PrintPage名称printDoc在当前上下文中不存在。一个更正应该是
elements.Add(directory+file.name)成功了,谢谢!没问题!您还可以使用file.FullName,这正是我要键入的内容。我会在回答中更正它。