您的位置:首页 > 文旅 > 旅游 > 网页传奇手游官网_旅游线路设计方案模板_百度竞价开户_aso如何优化

网页传奇手游官网_旅游线路设计方案模板_百度竞价开户_aso如何优化

2024/12/23 7:07:18 来源:https://blog.csdn.net/u013420428/article/details/143572880  浏览:    关键词:网页传奇手游官网_旅游线路设计方案模板_百度竞价开户_aso如何优化
网页传奇手游官网_旅游线路设计方案模板_百度竞价开户_aso如何优化

C# WPF 打印机

  • 打印机接口
    • 打印文本
    • 打印图片
    • 打印机属性对话框
    • 设置默认打印机
    • 搜索打印机

打印机接口

  • PrintServer
  • PrintQueue
  • PrintDocument

打印文本

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

打印图片

/// <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 falseImage 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);}}}

打印机属性对话框

private void btnPrintSettings_Click(object sender, RoutedEventArgs e)
{// 打印机属性对话框PrintDialog printDialog = new PrintDialog();if (printDialog.ShowDialog() == true){// 用户点击了打印Console.WriteLine("打印");}            
}

设置默认打印机

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

搜索打印机

private void btnPrintSearch_Click(object sender, RoutedEventArgs e)
{// 获取打印机列表并添加到ComboBoxPrintServer 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);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com