帮助类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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 ; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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";
1 2 3 4 5 6 7 8 9 10 | 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" ); } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本