C#实现自定义打印文字和图片的示例代码
作者:ViperL1 时间:2021-05-30 02:35:20
C#中打印其实就是自己绘图+调用系统打印函数,于是便有了以下操作
1.调用打印机设置
如果你想在打印前设置打印机属性(或者切换打印机),请务必添加这段代码,否则电脑会直接按照预设的设置进行打印(打印机都没法选)
//打印机设置
PrintDialog printDialog = new PrintDialog();
printDialog.Document = ptDoc;
printDialog.ShowDialog();
ptDoc就是打印的文件内容,会在后面解释
2.关联文档
等于我们需要新建一个空白的画布,然后把我们需要的文字,图片,线条依次摆上去
PrintDocument ptDoc = new PrintDocument();
ptDoc.DocumentName = "测试文件";//在队列中显示的名字
ptDoc.PrintPage += PtDoc_PrintPage;//关联内容生成方法
3.绘制内容
本次测试的内容分为:横排文字,竖排文字,图片,具体内容和位置可以通过修改参数进行调整
private void PtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.Clear(Color.White);
// 开始绘制文档
// 默认为横排文字
e.Graphics.DrawString("标题:打印测试",
new Font(new FontFamily("宋体"), 40, FontStyle.Bold),
System.Drawing.Brushes.Red,
170, 10);
// 横线
e.Graphics.DrawLine(Pens.Black, 20, 110, 800, 110);
// 竖排文字
e.Graphics.DrawString("内容",
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
20, 120,
new StringFormat(StringFormatFlags.DirectionVertical));
e.Graphics.DrawString( this.txtName.Text,
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
20, 200);
//e.Graphics.DrawImage(Image.FromFile(Path), 20, 250, 800, 100); //会造成失真
Image imgsample = Image.FromFile(Path);
Point pt = Zoom(imgsample.Width, imgsample.Height, 200);
e.Graphics.DrawImage(imgsample, 20, 250, pt.X, pt.Y);//直接打印可能出现太大打印不下
}
同时直接打印图片的话可能造成图片太大,可以写一个缩放方法来限制图片的最大尺寸
private Point Zoom(int x, int y,int maxWidth)
{
Point pt = new Point(x, y);
while (pt.X > maxWidth)
{
pt.X /= 2;
pt.Y /= 2;
}
return pt;
}
最后结果如下
来源:https://blog.csdn.net/weixin_37878740/article/details/124233949
标签:C#,打印,文字,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
设计模式系列之组合模式及其在JDK和MyBatis源码中的运用详解
2022-12-27 12:56:57
![](https://img.aspxhome.com/file/2023/6/68156_0s.png)
通过Java实现反向代理集群服务的平滑分配
2023-05-28 12:51:54
![](https://img.aspxhome.com/file/2023/9/61949_0s.png)
详解SpringMVC常用注解功能及属性
2021-12-29 02:49:23
Hibernate多对一单项关联
2023-05-10 18:16:11
![](https://img.aspxhome.com/file/2023/0/71880_0s.png)
java实战CPU占用过高问题的排查及解决
2023-01-14 21:46:49
![](https://img.aspxhome.com/file/2023/2/87372_0s.png)
mybatis like模糊查询特殊字符报错转义处理方式
2023-09-02 21:14:54
SpringBoot 2.x整合Log4j2日志的详细步骤
2022-08-05 09:50:19
![](https://img.aspxhome.com/file/2023/0/101760_0s.jpg)
Android Intent实现页面跳转的方法示例
2021-11-08 05:27:22
![](https://img.aspxhome.com/file/2023/9/93289_0s.png)
Java拦截器Interceptor和过滤器Filte的执行顺序和区别
2022-06-01 20:37:11
![](https://img.aspxhome.com/file/2023/2/61142_0s.jpg)
Spring基于AspectJ的AOP开发案例解析
2021-12-01 01:59:22
SpringCloud如何搭建一个多模块项目
2022-10-22 13:33:39
![](https://img.aspxhome.com/file/2023/1/78881_0s.png)
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
2022-05-24 17:14:13
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
Mybatis中的like模糊查询功能
2023-09-25 11:57:28
Java Socket实现传输压缩对象的方法示例
2022-12-18 06:52:30
Spring MVC文件配置以及参数传递示例详解
2023-06-03 23:29:48
![](https://img.aspxhome.com/file/2023/0/108460_0s.png)
spring配置文件解析失败报”cvc-elt.1: 找不到元素 ''beans'' 的声明”异常解决
2021-07-01 12:45:52
![](https://img.aspxhome.com/file/2023/9/89639_0s.png)
C#验证给定字符串形式日期是否合法的方法
2021-09-15 16:00:37
Spring Boot中lombok的安装与使用详解
2021-08-26 14:50:38
![](https://img.aspxhome.com/file/2023/5/66055_0s.jpg)
Java下SpringBoot创建定时任务详解
2023-10-03 01:25:23
![](https://img.aspxhome.com/file/2023/7/82307_0s.png)
Mybatis中xml的动态sql实现示例
2023-11-29 17:09:36