WPF實現打印用戶界面功能


方式一:
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用戶界面轉換為圖片

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

第二步,打印圖片

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)
        {
   System.Drawing.Image img = new Bitmap(filePath); e.Graphics.DrawImage(img, 50, 50); //e.Graphics.DrawString("aaa", DrawFont, brush, 600, 600); //繪制字符串 e.HasMorePages = false; }
//filePath=SaveWindowToImage(this,"c:\\test.bmp");

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM