欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
复制代码
方式一:
public
bool Print(string pathStr) { try { if (File.Exists(pathStr) == false) return false; var pr = new Process { StartInfo = { FileName = pathStr, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Verb = "Print"//printo、edit、open
//printto 调用默认打印机打印
//open 打开图片 } }; pr.Start();
return true; } catch (Exception) { return false; } } //等价于==》

private void PrintImage(string filePath)
{
  Process process = new Process();

   process.StartInfo.FileName ="filePath";

   string[] items=process.StartInfo.Verbs;
   process.StartInfo.Verb = "printto";
   process.StartInfo.CreateNoWindow = true;
   process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   process.Start();


}
private void 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); } }
复制代码

调用方式如下:

SaveWindowToImage(this,"c:\\test.bmp");
Print("c:\\test.bmp");

实现思路:用户界面转换为图片,打印图片文件。

 

方式二:

第一步,将WPF用户界面转换为图片

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;
        }

第二步,打印图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
        Font DrawFont = new Font("Arial", 22);
        private PrintDocument pd = new PrintDocument();
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            pd.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件
            //pd.PrinterSettings.PrinterName = "HP LaserJet Professional M1213nf MFP";        //打印机选择
            pd.Print();   // =>就似这么简单
        }
 
        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PicturePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {<br>           System.Drawing.Image img = new Bitmap(filePath);
            e.Graphics.DrawImage(img, 50, 50);
            //e.Graphics.DrawString("aaa", DrawFont, brush, 600, 600); //绘制字符串
            e.HasMorePages = false;
        }<br>//filePath=SaveWindowToImage(this,"c:\\test.bmp");

  

 

 

posted on   sunwugang  阅读(1717)  评论(0编辑  收藏  举报
编辑推荐:
· 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搭建本
历史上的今天:
2016-11-10 VS2012 InstallShield2013LimitedEdition激活
点击右上角即可分享
微信分享提示