WPF实现打印用户界面功能
方式一:
public bool Print(string pathStr)
{
try
{
if (File.Exists(pathStr) == false)
return false; var pr = new Process
{
StartInfo =
{
FileName = pathStr,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Verb = "Print"//printo、edit、open
//printto 调用默认打印机打印
//open 打开图片
}
};
pr.Start();
return true;
}
catch (Exception)
{
return false;
}
}
//等价于==》 private void PrintImage(string filePath)
{
Process process = new Process();
process.StartInfo.FileName ="filePath";
string[] items=process.StartInfo.Verbs;
process.StartInfo.Verb = "printto";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
}
private void SaveWindowToImage(Window window, string fileName)
{
FrameworkElement element = window.Content as FrameworkElement;
RenderTargetBitmap bmp = new RenderTargetBitmap((int)element.ActualWidth,
(int)element.ActualHeight, 96d, 96d, PixelFormats.Default);
bmp.Render(window); BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp)); using(FileStream stream=File.Open(fileName,FileMode.OpenOrCreate))
{
encoder.Save(stream);
}
}
调用方式如下:
SaveWindowToImage(this,"c:\\test.bmp");
Print("c:\\test.bmp");
实现思路:用户界面转换为图片,打印图片文件。
方式二:
第一步,将WPF用户界面转换为图片
private string SaveWindowToImage(Window window, string fileName)
{
FrameworkElement element = window.Content as FrameworkElement;
RenderTargetBitmap bmp = new RenderTargetBitmap((int)element.ActualWidth,
(int)element.ActualHeight, 96d, 96d, PixelFormats.Default);
bmp.Render(window); BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp)); using (FileStream stream = File.Open(fileName, FileMode.OpenOrCreate))
{
encoder.Save(stream);
}
return fileName;
}
第二步,打印图片
SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
Font DrawFont = new Font("Arial", 22);
private PrintDocument pd = new PrintDocument();
private void button3_Click(object sender, RoutedEventArgs e)
{
pd.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件
//pd.PrinterSettings.PrinterName = "HP LaserJet Professional M1213nf MFP"; //打印机选择
pd.Print(); // =>就似这么简单
} /// <summary>
/// 打印事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PicturePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image img = new Bitmap(filePath);
e.Graphics.DrawImage(img, 50, 50);
//e.Graphics.DrawString("aaa", DrawFont, brush, 600, 600); //绘制字符串
e.HasMorePages = false;
}
//filePath=SaveWindowToImage(this,"c:\\test.bmp");
WPF实现打印用户界面功能的更多相关文章
- WPF实现打印用户界面功能2
帮助类: using System; using System.Drawing.Printing; using System.IO; using System.Windows.Forms; names ...
- WPF实现打印功能
WPF实现打印功能 在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示.首先在VS中编辑一个图形(如下图所示). 将需要打印的内容放入同一个< ...
- WPF备忘录(6)WPF实现打印功能
在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示.首先在VS中编辑一个图形(如下图所示). 将需要打印的内容放入同一个<Canvas> ...
- 用WPF实现打印及打印预览
原文:用WPF实现打印及打印预览 应该说,WPF极大地简化了我们的打印输出工作,想过去使用VC++做开发的时候,打印及预览可是一件极麻烦的事情,而现在我不会再使用C++来做Windows的桌面应用了- ...
- C# WPF报表打印
前天我的一个同学由于打印报表而苦恼,所以就介绍了一下WPF的打印报表,希望能帮助到大家. 展示报表 1. 首先新建项“报表”,选定项目,右击,点击“添加”->“新建项”->“报表”
- WPF 流打印
原文:WPF 流打印 PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { Syst ...
- C# WPF发票打印
微信公众号:Dotnet9,网站:Dotnet9.问题或建议,请网站留言: 如果您觉得Dotnet9对您有帮助,欢迎赞赏 C# WPF发票打印 内容目录 实现效果 业务场景 编码实现 本文参考 源码下 ...
- EasyUI实现购物车、菜单和窗口栏等最常用的用户界面功能
一.EasyUI jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. easyui 提供建立现代化的具有交互性的 javascript 应用的必要的功能. 使用 e ...
- js开发打印证书功能
最近突然被加了要打印证书的功能的需求.其实打印功能很简单,直接调用window.print()就可以打印,只是这是最基本的打印,会打印当前页面的所有元素,而我们要的是局部打印,实现方法: 1.设置好开 ...
随机推荐
- Linux关闭透明大页配置
一.为何要关闭透明大页 A--MOS获取 . #翻译 由于透明超大页面已知会导致意外的节点重新启动并导致RAC出现性能问题,因此Oracle强烈建议禁用透明超大页面. 另外,即使在单实例数据库环境 ...
- [LeetCode&Python] Problem 455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 用jq修改css
$(".tag_add").css("background","#ffffff"); $(".tag_add").css ...
- C语言---辗转相除法 HDU 2503
Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T < =1000),表示有T组测试数据,然后是T行数据,每行包含四 ...
- 《DSP using MATLAB》Problem 5.32
代码: function [y] = ovrlpadd_v3(x, h, N) %% Overlap-Add method of block convolution %% -------------- ...
- ios 上浏览器返回上一页不会刷新页面问题,页面初始化的方法不执行
https://blog.csdn.net/yang450712123/article/details/79276102 https://blog.csdn.net/Chengbin_Huang/ar ...
- crash - JNI WARNING: input is not valid modified utf-8: illegal continuation byte
the key point is "Modified UTF-8" is not like "Regular UTF-8", a legal Rgular UT ...
- LG2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意 题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 ...
- eclipse操作(备忘)
myecplise破解 https://blog.csdn.net/by_xiaobai007/article/details/81177367 1.查看类路径 2.建立模板 window--pr ...
- POI事件模型处理execl导入功能(只支持07版本的execl)
由于通过new XSSFWorkbook 这种方式导入导致生产环境端口宕机.通过dump文件和javacore文件分析是导入功能导致的.解决办法:自己通过网上写的工具类,不知道是否存在bug. pac ...