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.设置好开 ...
随机推荐
- Android Hook框架adbi源码浅析(二)
二.libbase 其实上面加载完SO库后,hook的功能我们完全可以自己在动态库中实现.而adbi作者为了方便我们使用,编写了一个通用的hook框架工具即libbase库.libbase依然在解决两 ...
- 解决cocos2dx调用removeFromParent后报错问题
原因:cocos2dx的bug 解决办法: 放到action中,前面添加一个DelayTime延迟,代码如下 this->runAction(Sequence::create(DelayTime ...
- XXS level5
(1)用第四关的方法尝试,发现不行,查看页面源代码,发现on中间有了下划线 (2)查看PHP源代码 <?php ini_set("display_errors", 0); $ ...
- Atcode ABC105-C:Base -2 Number
C - Base -2 Number Time Limit: 2 sec / Memory Limit: 1024 MB Score : 300300 points Problem Statement ...
- 2017.5.10 MapReduce内部逻辑
MapReduce内部逻辑 Split:HDFS 中的数据以 Split 方式作为 MapReduce 的输入 Block 是 HDFS 术语,Split 是 MapReduce 术语 通常1个 Sp ...
- happens-before俗解
happens-before俗解 学习Java并发,到后面总会接触到happens-before偏序关系.初接触玩意儿简直就是不知所云,下面是经过一段时间折腾后个人对此的一点浅薄理解,希望对初接触的人 ...
- Blender界面及模式统计
Blender2.79b安装后默认界面分布: 所有窗口: 窗口: 3D View的9种模式:
- 初始While循环和for循环
While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假(False)不循环 格式: While 条件: 代码块 注意:在Whil ...
- 浅谈log4j-3-不同输出样式
public static void main(String[] args) { Logger logger=Logger.getLogger(Appendable.class); //设置输出等级 ...
- mysql深入
使用存储过程 create procedure productpricing() begin select avg(prod_price) as priceaverage from products; ...