WPF实现打印用户界面功能2
帮助类:
using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms; namespace PrintPage
{
/// <summary>
/// 文件打印方法
/// </summary>
public class PrintService
{
public PrintService()
{
//将事件处理函数添加到PrintDocument的PrintPage中
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
} // Declare the PrintDocument object.
private string streamType;
private Stream streamToPrint;
private PrintDocument docToPrint = new PrintDocument();//创建一个PrintDocument的实例 // This method will set properties on the PrintDialog object and
// then display the dialog.
public void StartPrint(Stream streamToPrint, string streamType)
{
this.streamToPrint = streamToPrint;
this.streamType = streamType;
PrintDialog printDialog = new PrintDialog();//创建一个PrintDialog的实例
printDialog.AllowSomePages = true;
printDialog.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
printDialog.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
DialogResult result = printDialog.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
// 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 document
switch (this.streamType)
{
case "txt":
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);
// Draw the content.
System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
text = streamReader.ReadToEnd();
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
break;
case "image":
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
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;
}
}
}
}
private Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close(); // 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes); return stream;
}
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;
}
//调用
private String filePath = "c:\\test.bmp";
private void button4_Click(object sender, RoutedEventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
FileInfo file = new FileInfo(path);
path = file.Directory.Parent.Parent.FullName + "\\MainWindow.xaml";
//path = file.Directory.FullName + "\\MainWindow.xaml";
Stream stream = FileToStream(filePath);
PrintService print = new PrintService();
print.StartPrint(stream, "image");
}
WPF实现打印用户界面功能2的更多相关文章
- WPF实现打印用户界面功能
方式一:public bool Print(string pathStr) { try { if (File.Exists(pathStr) == false) return false; var p ...
- 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.设置好开 ...
随机推荐
- idea 新建一个java项目并运行
这个只能算是个笔记,参考:参考文档 打开Idea,选择create new project,如图 或选择File ——> New——>Project... 出现以下界面,选中Java,然后 ...
- jsp-servlet(2)响应HTML文档-书籍管理系统
基础知识预备: 目标: 构建一个书籍管理系统,实现以下功能. 功能: 1 图书信息查询,(查) 2 书籍管理:添加书籍 3 书籍管理:修改书籍信息 4 书籍管理:删除书籍 一.预备工作 Book{ ...
- 【c++基础】遍历目录并写入txt文件-linux
前言 /****************************************************************************** * File: get_train ...
- shell脚本-预定义常量
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- HDU 1716:排列2(全排列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 《DSP using MATLAB》Problem 5.1
恰逢清明,这几天是清明时节雪纷纷,断崖式降温:又回到了老家,儿时上蹿下跳的核桃树,远去的故乡,远去的时代…… 用到的公式: 这里只放前两个小题的计算过程,都比较简单,细心就行.代码如下: %% --- ...
- python-基础-文件
一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件, 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 打开文件的模式有: r ,只读模式[默认模式 ...
- PyCharm下载安装
PyCharm 是一款功能强大的 Python 编辑器,具有跨平台性,鉴于目前最新版 PyCharm 使用教程较少,为了节约时间,来介绍一下 PyCharm 在 Windows下是如何安装的. 这是 ...
- test20190409 线段
题意 线段(segment) [题目描述] 给定n条线段,第i条线段的左端点为\(l_i\),右端点为\(r_i\).第i条线段覆盖了点x当且仅当\(l_i ≤x ≤ r_i\). 给定Q个询问,第i ...
- itcast-svn
svn介绍 1.1 svn服务器的工作方式 数据 库 服务 概念 使用数据库,连接服务,服务操作库 独立服务器方式: svnserve 借助Apache方式: mod_dav_svn ...