快捷搜索:

WPF的打印功能实现

先说一下思路:把控件保存为图片,然后把图片保存为xps文件,然后打印xps文件。保存为xps文件,下次再打印就不用从新调剂打印设置了。

控件保存为图片的措施:

//提取控件中的图像

RenderTargetBitmap bmp = new RenderTargetBitmap((int)控件.ActualWidth, (int)控件.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);bmp.Render(控件);

BitmapEncoder encoder = new PngBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(bmp));

string path = "指定文件路径 *.png";

using (FileStream file = new FileStream(path , FileMode.Create, FileAccess.Write))

{encoder.Save(file);

}

保存xps的措施

string path ="Data/xps/" + imgId + ".xps";

if (!Directory.Exists( "Data/xps"))Directory.CreateDirectory("Data/xps");

PageContent pageContent = new PageContent();

FixedPage fixedPage = new FixedPage();Image img = new Image();

img.Width = App.Photo_Width;img.Height = App.Photo_Height;

img.Stretch = Stretch.Fill;

img.Source = Printer.GetImage( "Data/NotePhotos/" + imgId + ".png");FixedPage.SetLeft(img, 0);

FixedPage.SetTop(img, 0);fixedPage.Width = App.Photo_Height;

fixedPage.Height = App.Photo_Width;img.RenderTransformOrigin = new Point(App.Photo_Height / (2 * App.Photo_Width), 0.5);

img.RenderTransform = new RotateTransform(90);

fixedPage.Children.Add((UIElement)img);Size sz = new Size(App.Photo_Height, App.Photo_Width);

fixedPage.Measure(sz);fixedPage.Arrange(new Rect(new Point(), sz));

fixedPage.UpdateLayout();((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);

XpsDocument doc = new XpsDocument(path, FileAccess.Write);

XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);writer.Write(fixedPage);

doc.Close();

上面的GetImage措施

///

/// 把文件读取到内存中,这样可以对文件进行其他操作///

///

///

public static BitmapSource GetImage(string url){

try{

using (BinaryReader reader = new BinaryReader(File.Open(url, FileMode.Open))){

FileInfo fi = new FileInfo(url);byte[] bytes = reader.ReadBytes((int)fi.Length);

reader.Close();BitmapImage bitmapImage = new BitmapImage();

bitmapImage.BeginInit();bitmapImage.StreamSource = new MemoryStream(bytes);

bitmapImage.EndInit();bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

return bitmapImage;}

}catch { return null; }

}

打印

///

/// 打印(应用默认打印机)///

///

要打印的图片编号///

宽度

///

高度public static void Print(string imgId)

{

//获取选中项if (imgId != string.Empty)

{string path ="Data/xps/" + imgId + ".xps";

if (File.Exists(path)){

//LocalPrintServer ps = new LocalPrintServer();//假如已存在该文件,则打印 应用系统默认打印机

PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();try { PrintSystemJobInfo xpsPringtJob = defaultPrintQueue.AddJob(imgId + ".xps", path, false); }

catch { }defaultPrintQueue.Dispose();

}else

{PageContent pageContent = new PageContent();

FixedPage fixedPage = new FixedPage();

Image img = new Image();img.Width = Photo_Width;

img.Height = Photo_Height;img.Stretch = Stretch.Fill; //Stretch.Fill;

img.Source = GetImage("Data/NotePhotos/" + imgId + ".png");

FixedPage.SetLeft(img, 0);FixedPage.SetTop(img, 0);

fixedPage.Width = Photo_Height;fixedPage.Height =Photo_Width;

img.RenderTransformOrigin = new Point((Photo_Height / 2) / Photo_Width, 0.5);img.RenderTransform = new RotateTransform(90);//扭转调剂图片

fixedPage.Children.Add((UIElement)img);

Size sz = new Size(Photo_Height, Photo_Width);fixedPage.Measure(sz);

fixedPage.Arrange(new Rect(new Point(), sz));fixedPage.UpdateLayout();

((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);

XpsDocument doc = new XpsDocument(path, FileAccess.Write);XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

writer.Write(fixedPage);doc.Close();

PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

try { PrintSystemJobInfo xpsPringtJob = defaultPrintQueue.AddJob(imgId + ".xps", path, false); }catch { }

defaultPrintQueue.Dispose();}

}

}

必要引用的一些命名空间:

using System.Printing;

using System.IO;

using System.Windows.Documents;

using System.Windows.Xps.Packaging;//添加引用=》ReachFramework

using System.Windows.Xps;

您可能还会对下面的文章感兴趣: