C# 使用Silverlight打印图片
原文:https://www.cnblogs.com/jiajiayuan/archive/2012/04/13/2444246.html
Silverlight中的打印只有一个类,那就是PrintDocment这个对象来实现。
下面我用两种方法来实现Silverlight的打印:
第一种:

- private void btnPrint_Click(object sender, RoutedEventArgs e)
- {
- PrintDocument document = new PrintDocument();
- // tell the API what to print
- document.PrintPage += (s, args) =>
- {
- args.PageVisual = GPrint;
- };
- // call the Print() with a proper name which will be visible in the Print Queue
- document.Print("Silverlight Print Application Demo");
- }

第二种:
实现方式也很简单,其实只需两个步骤即可完成,即绑定PrintDocument的PrintPage事件和调用Print方法。
- PrintDocument document = new PrintDocument();
- document.PrintPage += documentImage_PrintPage;
- document.Print("Image Document");
这个就完成了一个打印,其中PrintPage事件是最为重要的,因为整个打印的工作都是在这个事件中完成的,另外该事件的参数 PrintPageEventArgs构成了整个打印过程中的属性的设置;Print方法需要传递一个参数,参数为打印的文件的名称,在调用该方法的时候 开始触发一系列的打印事件。
PrintPageEventArgs类型的属性:
PrintableArea:获取一个Size类型的值,表示打印的范围,分别表示Height和Width,如果打印的部分超出了区域,则被截取。
PageMargins:获取打印页的Margin值。
PageVisual:设置要打印的对象,可以是一个TextBlock、Image,也可以是一个复杂的元素(Grid或者Canvas)。
HasMorePages:一个bool值,标识是否多页。
一个简单的例子:

- private void btnPrintImage_Click(object sender, RoutedEventArgs e)
- {
- PrintDocument document = new PrintDocument();
- document.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
- document.Print("Print Image");
- }
- void document_PrintPage(object sender, PrintPageEventArgs e)
- {
- Image imagePrint = new Image();
- imagePrint.Source = img.Source;
- imagePrint.Height = e.PrintableArea.Height;
- imagePrint.Width = e.PrintableArea.Width;
- e.PageVisual = imagePrint;
- e.HasMorePages = false;
- }

分页打印的例子:

- //当前打印的行的索引,用于遍历ListBox.Items
- private int listPrintIndex;
- private void btnPrintList_Click(object sender, RoutedEventArgs e)
- {
- //初始值为0
- listPrintIndex = 0;
- PrintDocument document = new PrintDocument();
- document.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
- document.Print("Print List");
- }
- //设置每一项之间的间距
- private int extraMargin = 50;
- void document_PrintPage(object sender, PrintPageEventArgs e)
- {
- //定义一个打印的元素
- Canvas printSurface = new Canvas();
- e.PageVisual = printSurface;
- //得到最顶端位置
- double topPosition = e.PageMargins.Top + extraMargin;
- //遍历当前的ListBox.Items
- while (listPrintIndex<lstPrint.Items.Count)
- {
- //实例化TextBlock用来存放ListItem的值
- TextBlock txt = new TextBlock();
- txt.FontSize = 30;
- //得到ListBox每一项的值
- txt.Text = lstPrint.Items[listPrintIndex].ToString();
- double measuredHeight = txt.ActualHeight;
- //如果打印的当前行高度不合适的话,则进行分页
- if (measuredHeight>(e.PrintableArea.Height- topPosition- extraMargin))
- {
- e.HasMorePages = true;
- return ;
- }
- //设置TextBlock在Canvas中的位置
- txt.SetValue(Canvas.TopProperty, topPosition);
- txt.SetValue(Canvas.LeftProperty, e.PageMargins.Left + extraMargin);
- //将TextBlock添加到打印的元素中去
- printSurface.Children.Add(txt);
- listPrintIndex++;
- //追加高度
- topPosition = topPosition + measuredHeight;
- }
- e.HasMorePages = false;
- }

有时我们会发现打印的图片并不完整,这样就需要一个类:

- public static class Extensions
- {
- public static void Print(this FrameworkElement element,
- string Document, HorizontalAlignment HorizontalAlignment,
- VerticalAlignment VerticalAlignment, Thickness PageMargin,
- bool PrintLandscape, bool ShrinkToFit, Action OnPrintComplete)
- {
- Print(new List<FrameworkElement>() { element }, Document,
- HorizontalAlignment, VerticalAlignment, PageMargin,
- PrintLandscape, ShrinkToFit, OnPrintComplete);
- }
- public static void Print<T>(this List<T> elements,
- string Document, HorizontalAlignment HorizontalAlignment,
- VerticalAlignment VerticalAlignment, Thickness PageMargin,
- bool PrintLandscape, bool ShrinkToFit, Action OnPrintComplete)
- {
- PrintDocument printDocument = new PrintDocument();
- PageMargin = PageMargin == null ? new Thickness(10) : PageMargin;
- Document = (string.IsNullOrEmpty(Document)) ? "Print Document" : Document;
- int currentItemIndex = 0;
- printDocument.PrintPage += (s, e) =>
- {
- if (!typeof(FrameworkElement).IsAssignableFrom(elements[currentItemIndex].GetType()))
- {
- throw new Exception("Element must be an " +"object inheriting from FrameworkElement");
- }
- FrameworkElement element = elements[currentItemIndex] as FrameworkElement;
- if (element.Parent == null || element.ActualWidth == double.NaN ||element.ActualHeight == double.NaN)
- {
- throw new Exception("Element must be rendered, " +
- "and must have a parent in order to print.");
- }
- TransformGroup transformGroup = new TransformGroup();
- //First move to middle of page... 首先移动到页面的中间
- transformGroup.Children.Add(new TranslateTransform() //TranslateTransform偏移动画
- {
- X = (e.PrintableArea.Width - element.ActualWidth) / 2,
- Y = (e.PrintableArea.Height - element.ActualHeight) / 8
- });
- double scale = 1;
- if (PrintLandscape) //如果打印空白 需要旋转
- {
- //Then, rotate around the center 然后旋转到中心
- transformGroup.Children.Add(new RotateTransform()
- {
- Angle = 90,
- CenterX = e.PrintableArea.Width / 2,
- CenterY = e.PrintableArea.Height / 2
- });
- if (ShrinkToFit) //如果自适应大小
- {
- if ((element.ActualWidth + PageMargin.Left +PageMargin.Right) > e.PrintableArea.Height) //如果宽度大于纸张的高度
- {
- //Math.Round 方法 将值舍入到最接近的整数或指定的小数位数。
- scale = Math.Round(e.PrintableArea.Height /(element.ActualWidth + PageMargin.Left + PageMargin.Right), 2);
- }
- if ((element.ActualHeight + PageMargin.Top + PageMargin.Bottom) > e.PrintableArea.Width) //如果高度大于纸张的宽度
- {
- double scale2 = Math.Round(e.PrintableArea.Width /(element.ActualHeight + PageMargin.Top + PageMargin.Bottom), 2);
- scale = (scale2 < scale) ? scale2 : scale;
- }
- }
- }
- else if (ShrinkToFit) //如果不打印空白并自适应大小 不需要旋转
- {
- //Scale down to fit the page + margin
- if ((element.ActualWidth + PageMargin.Left + PageMargin.Right) > e.PrintableArea.Width) //如果宽度大于纸张的宽度
- {
- scale = Math.Round(e.PrintableArea.Width /(element.ActualWidth + PageMargin.Left + PageMargin.Right), 2);
- }
- if ((element.ActualHeight + PageMargin.Top + PageMargin.Bottom) > e.PrintableArea.Height) //如果高度大于纸张的高度
- {
- double scale2 = Math.Round(e.PrintableArea.Height /(element.ActualHeight + PageMargin.Top + PageMargin.Bottom), 2);
- scale = (scale2 < scale) ? scale2 : scale;
- }
- }
- //Scale down to fit the page + margin
- if (scale != 1)
- {
- transformGroup.Children.Add(new ScaleTransform() //ScaleTransform缩放动画
- {
- ScaleX = scale,
- ScaleY = scale,
- CenterX = e.PrintableArea.Width / 2,
- CenterY = e.PrintableArea.Height / 2
- });
- }
- if (VerticalAlignment == VerticalAlignment.Top)
- {
- //Now move to Top
- if (PrintLandscape)
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = 0,
- Y = PageMargin.Top - (e.PrintableArea.Height -(element.ActualWidth * scale)) / 2
- });
- }
- else
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = 0,
- Y = PageMargin.Top - (e.PrintableArea.Height -(element.ActualHeight * scale)) / 2
- });
- }
- }
- else if (VerticalAlignment == VerticalAlignment.Bottom)
- {
- //Now move to Bottom
- if (PrintLandscape)
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = 0,
- Y = ((e.PrintableArea.Height -(element.ActualWidth * scale)) / 2) - PageMargin.Bottom
- });
- }
- else
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = 0,
- Y = ((e.PrintableArea.Height -(element.ActualHeight * scale)) / 2) - PageMargin.Bottom
- });
- }
- }
- if (HorizontalAlignment == HorizontalAlignment.Left)
- {
- //Now move to Left
- if (PrintLandscape)
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = PageMargin.Left - (e.PrintableArea.Width -(element.ActualHeight * scale)) / 2,
- Y = 0
- });
- }
- else
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = PageMargin.Left - (e.PrintableArea.Width -(element.ActualWidth * scale)) / 2,
- Y = 0
- });
- }
- }
- else if (HorizontalAlignment == HorizontalAlignment.Right)
- {
- //Now move to Right
- if (PrintLandscape)
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = ((e.PrintableArea.Width -(element.ActualHeight * scale)) / 2) - PageMargin.Right,
- Y = 0
- });
- }
- else
- {
- transformGroup.Children.Add(new TranslateTransform()
- {
- X = ((e.PrintableArea.Width -(element.ActualWidth * scale)) / 2) - PageMargin.Right,
- Y = 0
- });
- }
- }
- e.PageVisual = element;
- e.PageVisual.RenderTransform = transformGroup;
- //Increment to next item,
- currentItemIndex++;
- //If the currentItemIndex is less than the number of elements, keep printing
- e.HasMorePages = currentItemIndex < elements.Count;
- };
- printDocument.EndPrint += delegate(object sender, EndPrintEventArgs e)
- {
- foreach (var item in elements)
- {
- FrameworkElement element = item as FrameworkElement;
- //Reset everything...
- TransformGroup transformGroup = new TransformGroup();
- transformGroup.Children.Add(new ScaleTransform() { ScaleX = 1, ScaleY = 1 }); //缩放动画
- transformGroup.Children.Add(new RotateTransform() { Angle = 0 }); //旋转动画
- transformGroup.Children.Add(new TranslateTransform() { X = 0, Y = 0 }); //偏移动画
- element.RenderTransform = transformGroup;
- }
- //Callback to complete
- if (OnPrintComplete != null)
- {
- OnPrintComplete();
- }
- };
- printDocument.Print(Document);
- }
- }

调用这个类:

- private void btnPrint_Click(object sender, RoutedEventArgs e)
- {
- Extensions.Print(GPrint, "MyPrint",
- HorizontalAlignment.Center, VerticalAlignment.Top,
- new Thickness(10, 0, 10, 0), true, true, null);
- }

这样就能完整的打印了,不过打印出来的效果可能是横向的。
C# 使用Silverlight打印图片的更多相关文章
- Silverlight打印注意事项
1.Silverlight的打印功能从版本5开始才支持矢量打印,这不但要求打印机支持矢量打印,而且还要安装相应的打印驱动程序. 测试你的打印机是否支持矢量打印,可以参考:如何用C#代码检测打印机和驱动 ...
- 关于silverlight打印模糊的问题
今天做silverlight打印实现时,发现一个问题,就是sl打印处理的文字很模糊 这样肯定不行撒,于是开始找解决办法,首先想到的是silverlight中文显示的问题,好 ...
- C# 热敏打印机 Socket 网络链接 打印 图片
C# 热敏打印机 Socket 网络链接 打印 图片 (一) http://www.cnblogs.com/rinack/p/4838211.html C# 热敏打印机 Socket 网络链接 打印 ...
- silverlight 打印
加引用: using System.Windows.Printing; xaml文件里: //定义图片和文本打印变量 PrintDocument printImage; public BeginCo ...
- LODOP用ADD_PRINT_IMAGE语句缩放打印图片
LODOP提高输出图片质量的方法:1.用ADD_PRINT_IMAGE语句打印图片,而且img元素的width和height属性要去掉或者设置足够大,这样就可以让下载引擎传给Lodop图片质量足够好; ...
- Python+OpenCV图像处理(二)——打印图片属性、设置图片存储路径、电脑摄像头的调取和显示
一. 打印图片属性.设置图片存储路径 代码如下: #打印图片的属性.保存图片位置 import cv2 as cv import numpy as np #numpy是一个开源的Python科学计算库 ...
- Silverlight 中图片路径的设置
在Silverlight中图片的设置方法有几种 如上图在一个工程中有个images文件夹,buttons.xaml页面中的Image控件要引用一张图片 第一种方法 xaml: <Image x: ...
- C# 热敏打印机 小票打印机 打印图片
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: 因为我们这里主要是打印条形码和二维码,所以以条形码和二维 ...
- C#热敏打印图片 串口打印图片
原文:C#热敏打印图片 串口打印图片 如图,一步一步慢慢调出来的 //串口通信类 public System.IO.Ports.SerialPort serialPort = null; serial ...
随机推荐
- 7——C++类的使用
定义了一个类之后,便可以如同用int.double等类型符声明简单变量一样,创建该类的对象,称为类的实例化. 类的定义实际上是定义了一种类型,类不接收或存储具体的值,只作为生成具 ...
- this与super的区别
调用super()的语句必须要写在子类构造方法的第一行. super()是在子类中调用父类的构造方法:this()是在同一类中调用其它方法. super()和this()都需要放在构造函数的第一行. ...
- kettle 通过JDBC 连接SQL Server(Error occurred while trying to connect to the database)
在连接数据(MS SQLServer 2008)发现:Error occurred while trying to connect to the database 然后找资料看,都不是问题所在,最后一 ...
- Redis-缓存击穿/穿透/雪崩
缓存击穿/穿透/雪崩 Intro 使用缓存需要了解几个缓存问题,缓存击穿.缓存穿透以及缓存雪崩,需要了解它们产生的原因以及怎么避免,尤其是当你打算设计自己的缓存框架的时候需要考虑如何处理这些问题. 缓 ...
- Delphi GridPanel Percent百分比设置
可能很多人都有这个困扰,为什么每次设置一个百分比后,值都会改变,只有设置成absolute才会正常,经摸索发现,是因为精度引起,设置percent的时候,需要将精确到多个小数位.如要有3列,需要设置 ...
- 数组与List互转的坑
一.数组转List 非原始类型的数组转List有很多种方式:假设有Integer[] arr = {"a", "b", "c"}; 1.Li ...
- java扫描仪上传文件
问题: 项目中有一个功能,原来是用ckfinder做的,可以选择本地图片上传至服务器,然后将服务器的图片显示在浏览器中,并可以将图片地址保存到数据库:现在客户觉得麻烦,提出连接扫描仪扫描后直接上传至服 ...
- Leetcode_131. Palindrome Partitioning_[DFS]
题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 【Java】Java URLDecoder异常Illegal hex characters in escape (%)
如果收到的HTTP请求参数(URL中的GET请求)中有一个字符串,是中文,比如“10%是黄段子”,服务器段使用URLDecoder.decode就会出现此异常.URL只能使用英文字母.阿拉伯数字和某些 ...
- Delphi实现获取磁盘空间大小的方法
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ...