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.设置好开 ...
随机推荐
- [转]Deep Reinforcement Learning Based Trading Application at JP Morgan Chase
Deep Reinforcement Learning Based Trading Application at JP Morgan Chase https://medium.com/@ranko.m ...
- 安装12C小问题及pdb表空间配置
安装12C小问题及pdb表空间配置 一.安装 1.RPM包 #安装12C需要安装的rpm包,官网搜索,做个记录 bc binutils-2.23.52.0.1-12.el7(x86_64) compa ...
- Spring cron表达式详解
一个cron表达式有6个必选的元素和一个可选的元素,各个元素之间是以空格分隔的,从左至右,这些元素的含义如下表所示: 代表含义 是否必须 允许的取值范围 允许的特殊符号 秒 是 0-59 , - * ...
- django 浅谈CSRF(Cross-site request forgery)跨站请求伪造
浅谈CSRF(Cross-site request forgery)跨站请求伪造(写的非常好) 本文目录 一 CSRF是什么 二 CSRF攻击原理 三 CSRF攻击防范 回到目录 一 CSRF是什么 ...
- GinKgoCTF-Crypto
一:古典密码 一只小羊跳过了栅栏,密码如下: GpKlCeT_FC{rAy_pStiym} 栅栏解密——>11栏 二:滴滴滴 --./-.-/-.-./-/..-./----.--/-../.. ...
- C++ 作业 (循环链表构建队列)
/* author screen name Andromeda_Galaxy; chinese name 杨子俊 */ #include<bits/stdc++.h> using name ...
- springboot配置文件启动顺序
[1]项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 1.–f ...
- Thinkphp,Jquery,Ajax异步发布
1.在提交表单的HTML页面的<head>中定义一个变量供Jquery使用 <script type='text/javascript'>var handleUrl='< ...
- hashCode()方法 和 hash()方法
String str = "abc"; String str1 = "abc"; System.out.println(str == str1); //true ...
- react-static 基于react 渐进式静态站点生成框架
react-static 是一个不错的基于react 开发的静态站点生成框架,可以用来替代create-react-app 包含的特性 100% react 很快的构建以及性能 自动代码以及数据分离 ...