C# WPF 打印机

C# WPF 打印机

打印机接口

  • PrintServer
  • PrintQueue
  • PrintDocument

打印文本

csharp 复制代码
/// <summary>
/// 打印文本
/// </summary>
/// <param name="text"></param>
/// <param name="width"></param>
/// <param name="height"></param>
private void PrintText(string text, int width, int height)
{
    // 创建一个PrintDocument对象
    PrintDocument pd = new PrintDocument();
    // pd.DefaultPageSettings.Landscape = true;
    // pd.DefaultPageSettings.PrinterSettings.PrinterName = "Gprinter  GP-1134T";  // 指定打印机

    // 设置打印机任务的事件处理
    pd.PrintPage += (sender, e) =>
    {
        // 创建一个Bitmap对象,用于绘制文本                
        using (Bitmap bitmap = new Bitmap(width, height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // 使用白色填充背景
                g.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);

                // 设置打印文本的格式
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                // 在位图上绘制文本
                g.DrawString(text, new Font("Arial", 12), System.Drawing.Brushes.Black, new RectangleF(0, 0, width, height), format);

                // 将位图发送到打印机
                e.Graphics.DrawImage(bitmap, 0, 0);                        
            }
        }   
    };

    // 开始打印
    try
    {
        pd.Print();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

打印图片

csharp 复制代码
 /// <summary>
 /// 打印图片
 /// </summary>
 /// <param name="imagePath">图片文件路径</param>
 /// <param name="width">打印宽度像素</param>
 /// <param name="height">打印高度像素</param>
 private void PrintImage(string imagePath, int width, int height)
 {
     if (File.Exists(imagePath))
     {
         PrintDocument pd = new PrintDocument();
         pd.DefaultPageSettings.Landscape = true;
         //pd.DefaultPageSettings.PrinterSettings.PrinterName = "Gprinter  GP-1134T";


         Image image = Image.FromFile(imagePath);
         // 设置打印机任务的事件处理
         pd.PrintPage += (sender, e) =>
         {
             // 创建一个Bitmap对象,用于绘制图片
             using (Bitmap bitmap = new Bitmap(width, height))
             {
                 using (Graphics g = Graphics.FromImage(bitmap))
                 {
                     // 使用白色填充背景
                     g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, width, height);

                     // 在位图上绘制图片
                     g.DrawImage(image, 0, 0);

                     // 将位图发送到打印机
                     e.Graphics.DrawImage(bitmap, 0, 0);
                 }
             }
#if false
             Image i = Image.FromFile(imagePath);
             System.Drawing.Rectangle m = e.MarginBounds;
             if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
             {
                 m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
             }
             else
             {
                 m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
             }
             e.Graphics.DrawImage(i, m);                    
#endif
         };

         // 开始打印
         try
         {
             pd.Print();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }

     }
 }

打印机属性对话框

csharp 复制代码
private void btnPrintSettings_Click(object sender, RoutedEventArgs e)
{
    // 打印机属性对话框
    PrintDialog printDialog = new PrintDialog();

    if (printDialog.ShowDialog() == true)
    {
        // 用户点击了打印
        Console.WriteLine("打印");
    }            
}

设置默认打印机

csharp 复制代码
/// <summary>
/// 设置默认打印机
/// </summary>
/// <param name="printerName"></param>
public static void SetDefaultPrinter(string printerName)
{
    PrintServer printServer = new PrintServer(); // 默认的打印服务器
    PrintQueue printQueue = new PrintQueue(printServer, printerName); // 根据打印机名称创建打印队列

    // 设置为默认打印机
    printQueue.Commit();
}

搜索打印机

csharp 复制代码
private void btnPrintSearch_Click(object sender, RoutedEventArgs e)
{
    // 获取打印机列表并添加到ComboBox
    PrintServer printServer = new PrintServer();
    PrintQueueCollection printQueues = printServer.GetPrintQueues();

    cmboxPrintList.Items.Clear(); // ComboBox控件
    foreach (PrintQueue p in printQueues)
    {
        cmboxPrintList.Items.Add(p.Name);
    }
}

// 选择打印机
private void cmboxPrintList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string pName = cmboxPrintList.SelectedItem as string;

    Console.WriteLine("选择打印机");

    if (pName != null)
    {
        Console.WriteLine("默认打印机:" + pName);
        SetDefaultPrinter(pName);
    }
}
相关推荐
暖馒8 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
R1nG86310 小时前
HCCL vs NCCL代码级对比 hccl/algorithms/ vs nccl/src/collectives/ Ring算法实现差异
wpf·cann
风指引着方向13 小时前
归约操作优化:ops-math 的 Sum/Mean/Max 实现
人工智能·wpf
听麟17 小时前
HarmonyOS 6.0+ 跨端智慧政务服务平台开发实战:多端协同办理与电子证照管理落地
笔记·华为·wpf·音视频·harmonyos·政务
听麟20 小时前
HarmonyOS 6.0+ APP AR文旅导览系统开发实战:空间定位与文物交互落地
人工智能·深度学习·华为·ar·wpf·harmonyos
聆风吟º1 天前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann
无心水2 天前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
LZL_SQ2 天前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann
User_芊芊君子3 天前
【分布式训练】CANN SHMEM跨设备内存通信库:构建高效多机多卡训练的关键组件
分布式·深度学习·神经网络·wpf
就是有点傻4 天前
WPF按钮走马灯效果
wpf