本方法适用于有打印驱动的打印机打印。直接用电脑默认打印机进行打印文字和图片。

首先安装打印机驱动,然后在设备和打印机中将要用的打印机设为默认打印机,然后调用该方法即可

Pulic Void Print()
{PrintService ps = new PrintService();ps.StartPrint("33333","txt");//打印文字ps.StartPrint(Image.FromFile(Application.StartupPath+"\\2.jpeg"), "image");//打印图片
}

PrintService类源码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 系统打印demo
{public class PrintService{public PrintService(){//// TODO: 在此处添加构造函数逻辑//this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);}//将事件处理函数添加到PrintDocument的PrintPage中// Declare the PrintDocument object.private System.Drawing.Printing.PrintDocument docToPrint =new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例private string streamType;private string streamtxt;private Image streamima;// This method will set properties on the PrintDialog object and// then display the dialog.public void StartPrint(string txt, string streamType){this.streamType = streamType;this.streamtxt = txt;// Allow the user to choose the page range he or she would// like to print.System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。PrintDialog1.AllowSomePages = true;// Show the help button.PrintDialog1.ShowHelp = true;// Set the Document property to the PrintDocument for // which the PrintPage Event has been handled. To display the// dialog, either this property or the PrinterSettings property // must be set PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框,如果不要注释即可,直接调用docToPrint.Print()// If the result is OK then print the document.if (result == DialogResult.OK){docToPrint.Print();//开始打印}}public void StartPrint(Image ima, string streamType){this.streamType = streamType;this.streamima = ima;// Allow the user to choose the page range he or she would// like to print.System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。PrintDialog1.AllowSomePages = true;// Show the help button.PrintDialog1.ShowHelp = true;// Set the Document property to the PrintDocument for // which the PrintPage Event has been handled. To display the// dialog, either this property or the PrinterSettings property // must be set PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框,如果不要注释即可,直接调用docToPrint.Print()// If the result is OK then print the document.if (result == DialogResult.OK){docToPrint.Print();//开始打印}}// The PrintDialog will print the document// by handling the document's PrintPage event.private void docToPrint_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数{// Insert code to render the page here.// This code will be called when the control is drawn.// The following code will render a simple// message on the printed documentswitch (this.streamType){case "txt":string text = null;System.Drawing.Font printFont = new System.Drawing.Font("Arial", 35, System.Drawing.FontStyle.Regular);// Draw the content.text = streamtxt;e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);break;case "image":System.Drawing.Image image = streamima;int x = e.MarginBounds.X;int y = e.MarginBounds.Y;int width = image.Width;int height = image.Height;if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)){width = e.MarginBounds.Width;height = image.Height * e.MarginBounds.Width / image.Width;}else{height = e.MarginBounds.Height;width = image.Width * e.MarginBounds.Height / image.Height;}System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);break;default:break;}}}
}

C#调用系统默认打印机打印文字和图片相关推荐

  1. Android调用系统默认打印机并反射获取打印任务状态

    Android4.4通过PrintManger调用系统默认打印服务的默认打印机进行打印任务,并通过反射调用PrintManager的getPrintJob方法获取打印任务状态.不说太多,直接上代码.第 ...

  2. uniapp微信小程序连接蓝牙打印机 打印文字、图片

    首先感谢几位的文章分享 https://blog.csdn.net/guairena/article/details/127941515 https://blog.csdn.net/qq_379700 ...

  3. java原生打印实现自动调用系统默认打印机

    最近公司项目里要实现一个自动打印的功能,还不让引入网上造好的轮子,所以只能去研究Java自带的打印,网上好多的文章写得都不是很详细,找了很久才找到能够完美实现功能的方法,记录一下 首先是一个继承了Pr ...

  4. 打印技术之获取及设置系统默认打印机

    http://www.cnblogs.com/menlsh/p/4189126.html 本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用En ...

  5. 【打印技术01】获取及设置系统默认打印机

    本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: 1 ...

  6. 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机

    本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: 1 ...

  7. linux下打印图片不显示出来的,为什么打印机打印不了图片_解决打印机打印不了图片的方法-系统城...

    在办公室上班的都会接触到打印机,大家在使用打印机会碰到打印不出图片的情况,为什么会这样呢?出现这种情况排除打印机驱动外,一般是由于打印机的设置不当造成的.针对此疑问,小编教你解决打印机打印不了图片的方 ...

  8. 计算机怎么查文件打印记录表,win10系统查看打印机打印历史记录的设置教程

    有关win10系统查看打印机打印历史记录的操作方法想必大家有所耳闻.但是能够对win10系统查看打印机打印历史记录进行实际操作的人却不多.其实解决win10系统查看打印机打印历史记录的问题也不是难事, ...

  9. 打印U盘文件计算机有记录吗,技术员教你win10系统查看打印机打印历史记录的问题...

    技术员教你win10系统查看打印机打印历史记录的问题? 很多朋友安装win10系统后,在使用的过程中会遇到对win10系统查看打印机打印历史记录进行设置的情况,可能有很多用户还是不能自己对win10系 ...

最新文章

  1. linux注册硬盘中断,基于Linux底层硬盘中断辅助代码的分析与设计
  2. mysql Navcat触发器生成订单号
  3. python爬取全国社会组织查询网站
  4. php的filter input,php中filter_input函数用法分析
  5. 如何在命令长度受限的情况下成功get到webshell(函数参数受限突破、mysql的骚操作)...
  6. https://docs.python.org/3/
  7. java静态引用_Java开发中静态方法引用和实例方法引用案例详细讲解
  8. 获取.propertys文件获取文件内容
  9. 用JAVA写出1到999的随性化数_请详细翻译一个简单的JAVA程序问题
  10. Q116:PBRT-V3场景描述文件.pbrt格式解析
  11. 163邮箱注册申请入口,申请163的邮箱账号
  12. 路由器、DDNS、花生壳
  13. E45: ‘readonly‘ option is set (add ! to override)解决办法
  14. 商品详情页中间显示不出来
  15. 条件概率下的全概率公式
  16. SQL语句查询拼音码
  17. SQL Server全程
  18. 数据库拆分的理解和案例(详细版)
  19. Sequoia(基于JDBC的数据库集群中间件)用户手册
  20. 【usb】USB Core功能解析(一)-------linux那些事儿之我是usb

热门文章

  1. 如何写出优质干净的代码,这6个技巧你不能错过!
  2. Spring Security 之集群Session配置
  3. git 创建分支,更改并提交
  4. php 字符串处理
  5. POJ 3376 Finding Palindromes(扩展kmp+trie)
  6. Backbone学习日记[1]:感性认识
  7. .net mysql中文乱码解决办法
  8. Windows Phone 7 WebBrowser 中文乱码问题
  9. 巧用二进制,让性能提升100倍,让存储空间减少100倍
  10. 13本干货书籍,百余篇原创来了