其他分享
首页 > 其他分享> > WPF 打印界面控件内容

WPF 打印界面控件内容

作者:互联网

    public class PrintDialogHelper
    {
        private const string PrintServerName = "DESKTOP-49LV5U6";
        private const string PrintName = "80mm Series Printer";

        public PrintDialogHelper()
        {
        }

        /// <summary>
        /// 打印控件
        /// </summary>
        /// <param name="element"></param>
        public void PrintVisual(FrameworkElement element)
        {
            var printDialog = new PrintDialog();
            SetPrintProperty(printDialog);
            var printQueue = SelectedPrintServer(PrintServerName, PrintName);
            if (printQueue != null)
            {
                printDialog.PrintQueue = printQueue;
                printDialog.PrintVisual(element, "QRPrint");
            }
        }

        /// <summary>
        /// 查找并获取打印机
        /// </summary>
        /// <param name="printerServerName">服务器名字: Lee-pc</param>
        /// <param name="printerName">打印机名字:Hp laserjet m1522 mfp series pcl 6 </param>
        /// <returns></returns>
        public PrintQueue SelectedPrintServer(string printerServerName, string printerName)
        {
            try
            {
            //    var printers = PrinterSettings.InstalledPrinters;//获取本机上的所有打印机
            //    PrintServer printServer = null;

            //    foreach (string printer in printers)
            //    {
            //        if (printer.Contains(printerName))
            //            printServer = new PrintServer("\\\\" + printerServerName);
            //    }

            //    if (printServer == null) return null;//没有找到打印机服务器

            //    var printQueue = printServer.GetPrintQueue(printerName);
                var printQueue = LocalPrintServer.GetDefaultPrintQueue();
            return printQueue;
            }
            catch (Exception)
            {
                return null;//没有找到打印机
            }
        }

        /// <summary>
        /// 设置打印格式
        /// </summary>
        /// <param name="printDialog">打印文档</param>
        /// <param name="pageSize">打印纸张大小 a4</param>
        /// <param name="pageOrientation">打印方向 竖向</param>
        public void SetPrintProperty(PrintDialog printDialog,  PageOrientation pageOrientation = PageOrientation.Portrait)
        {
            var printTicket = printDialog.PrintTicket;
            printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.Unknown,198, 842);
            printTicket.PageOrientation = pageOrientation;//默认竖向打印
        }
    }

参考链接: https://blog.csdn.net/liuruxin/article/details/28656829?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-28656829-blog-118187864.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-28656829-blog-118187864.pc_relevant_antiscanv2&utm_relevant_index=2

 

一张图片的打印出来的实际尺寸是由此图片的像素和分辨率共同决定的,像素(Pixel)是指构成图片的小色点,分辨率(单位DPI)是指每英寸(Inch)上的像素数量,可以看做是这些小色点的分布密度;像素相同时,分辨率越高则像素密度越大,实际打印尺寸越小,图像也越细腻。

实际尺寸(英寸)=像素/分辨率; 1英寸=2.54厘米; 如一张图片宽为600像素,分辨率为300,那么实际打印宽度为:600/300=2英寸,约为5厘米。

 像素尺寸换算公式:  https://www.gaitubao.com/tools/pixel2cm.html

 

标签:printQueue,控件,界面,打印,像素,blog,var,WPF,printDialog
来源: https://www.cnblogs.com/NetNotes/p/16308379.html