You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现文件夹内图片的批量静默打印(支持指定打印机)

实现文件夹内图片的静默批量打印方案

原代码依赖系统默认图片查看器的print动词触发打印,即便设置WindowStyle.Hidden,仍会弹出打印窗口——原因是该动词会唤起关联程序的交互打印流程。以下是两种可行的静默打印实现方案:

方案一:使用.NET原生打印API(完全静默,可控性强)

直接通过System.Drawing.Printing命名空间的API打印图片,不依赖外部程序,支持指定打印机、调整打印参数,全程无弹窗。

注意事项

  • .NET Framework 可直接引用System.Drawing;.NET Core/.NET 5+ 需要安装System.Drawing.Common NuGet包。
  • 需处理图片加载、打印事件绑定,确保打印流程静默执行。

代码示例

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

class SilentImagePrinter
{
    static void Main()
    {
        // 目标文件夹路径
        string folderPath = @"C:\Users\startup\Desktop\New folder";
        // 可选:指定目标打印机名称,留空则用默认打印机
        string targetPrinter = "你的打印机名称";

        // 获取文件夹内所有JPEG图片
        string[] imageFiles = Directory.GetFiles(folderPath, "*.jpeg");

        foreach (string filePath in imageFiles)
        {
            PrintImageSilently(filePath, targetPrinter);
        }
    }

    static void PrintImageSilently(string imagePath, string printerName)
    {
        using (PrintDocument pd = new PrintDocument())
        {
            // 指定打印机
            if (!string.IsNullOrEmpty(printerName))
            {
                pd.PrinterSettings.PrinterName = printerName;
                // 验证打印机是否存在
                if (!pd.PrinterSettings.IsValid)
                {
                    throw new Exception($"指定打印机 {printerName} 不存在");
                }
            }

            // 绑定打印事件
            pd.PrintPage += (sender, e) =>
            {
                using (Image img = Image.FromFile(imagePath))
                {
                    // 适配打印区域(可根据需求调整缩放逻辑)
                    Rectangle printArea = e.MarginBounds;
                    e.Graphics.DrawImage(img, printArea);
                }
            };

            // 静默打印(无对话框)
            pd.Print();
        }
    }
}

方案二:改进ShellExecute方式(依赖系统关联程序)

使用printto动词替代print,直接指定打印机名称,可避免弹出打印对话框,但需确保系统对图片格式的关联程序支持该动词(多数主流图片查看器支持)。

代码示例

using System;
using System.Diagnostics;
using System.IO;

class ShellSilentPrinter
{
    static void Main()
    {
        DirectoryInfo d = new DirectoryInfo(@"C:\Users\startup\Desktop\New folder");
        FileInfo[] Files = d.GetFiles("*.jpeg");
        // 指定目标打印机名称
        string targetPrinter = "你的打印机名称";

        foreach (FileInfo file in Files)
        {
            ProcessStartInfo psiPrint = new ProcessStartInfo();
            psiPrint.Verb = "printto";
            psiPrint.FileName = file.FullName;
            // 传递打印机名称作为参数
            psiPrint.Arguments = $"\"{targetPrinter}\"";
            psiPrint.WindowStyle = ProcessWindowStyle.Hidden;
            psiPrint.UseShellExecute = true;
            // 等待打印进程启动后退出(可选,避免资源占用)
            using (Process p = Process.Start(psiPrint))
            {
                p.WaitForExit(1000);
            }
        }
    }
}

获取已安装打印机列表

如果需要让用户选择打印机,可通过以下代码枚举所有可用打印机:

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer);
}

内容的提问来源于stack exchange,提问作者fast2021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:30:03