码迷,mamicode.com
首页 > Windows程序 > 详细

C# 打印机

时间:2018-08-16 13:35:33      阅读:414      评论:0      收藏:0      [点我收藏+]

标签:microsoft   mil   process   ntb   obj   mes   setting   size   设置   

程序控制打印机打印存在问题,即不按默认参数打印。

一开始按如下代码打印,会弹出点击确认的对话框,为了不弹窗,采用PrintDocument类打印

        //ProcessStartInfo info = new ProcessStartInfo(@"D:\Demo\picWorldCup\Test01.png");
            ////info.Arguments = string.Format("-noquery -landscape -printer \"{0}\" \"{1}\"",
            //// "Canon MG3600 series Printer", @"D:\Demo\picWorldCup\Test01.png");
            //info.Verb = "Print";
            //info.CreateNoWindow = true;
            //info.WindowStyle = ProcessWindowStyle.Hidden;
            //Process.Start(info);

PrintDocument打印代码如下

    class Program
    {


        static void Main(string[] args)
        {
            //bool b = true;

            //ProcessStartInfo info = new ProcessStartInfo(@"D:\Demo\picWorldCup\Test01.png");
            ////info.Arguments = string.Format("-noquery -landscape -printer \"{0}\" \"{1}\"",
            //// "Canon MG3600 series Printer", @"D:\Demo\picWorldCup\Test01.png");
            //info.Verb = "Print";
            //info.CreateNoWindow = true;
            //info.WindowStyle = ProcessWindowStyle.Hidden;
            //Process.Start(info);

            //string printWin = "打印图片";
            //string printBtn = "取消";

            //Stopwatch sw = new Stopwatch();
            //sw.Start();

            //while(b)
            //{
            //    if(sw.ElapsedMilliseconds>200)
            //    {
            //        sw.Stop();
            //        b = false;
            //    }
            //}

            //ApiUtility.OnRunClick(printWin, printBtn);

            string printFile = args[0];
            //string printerName = args[1];

            int width;
            int height;
            if (!int.TryParse(args[1], out width))
            {
                width = 355;
            }
            if (!int.TryParse(args[2], out height))
            {
                height = 500;
            }

            //Console.WriteLine("Print size:" + width + " " + height);
            PrintDocument pd = new PrintDocument();
            pd.PrintController = new StandardPrintController();

            //Margins margin = new Margins(0, 0, 0, 0);
            //pd.DefaultPageSettings.Margins = margin;

            //pd.DefaultPageSettings.Color = false;
            //pd.DefaultPageSettings.PaperSize = new PaperSize("L", 44, 63);
            //pd.DefaultPageSettings.Margins = margin;
            //pd.PrinterSettings.PrinterName = printerName;

            pd.PrintPage += (o, e) =>
            {
                Image img = Image.FromFile(printFile);
                int imgWidth = (int)(img.Height * 0.7f);
                e.Graphics.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imgWidth, img.Height), GraphicsUnit.Pixel);//通用
                //Console.WriteLine("image size:" + imgWidth + " " + img.Height);
                //e.Graphics.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
                //Console.WriteLine("W:{0}  H:{1}", img.Width, img.Height);
            };

            try
            {
                pd.Print();
                Console.WriteLine("success");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed:" + ex.Message);
            }
            //btnPrint_Click();

            Console.ReadKey();

        }

        public static void btnPrint_Click()
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintController = new StandardPrintController();

            //pd.OriginAtMargins = true;
            //Margins margin = new Margins(0, 0, 0, 0);

            //pd.DefaultPageSettings.Color = false;

            //pd.DefaultPageSettings.PaperSize = new PaperSize("L", 89, 127);
            //pd.PrinterSettings.PrinterName = "Canon MG3600 series Printer";
            //pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";

            //pd.DefaultPageSettings.Margins = margin;
            Console.WriteLine("Margin:" + pd.DefaultPageSettings.Margins);
            Console.WriteLine("Bound:" + pd.DefaultPageSettings.Bounds);
            Console.WriteLine("xHardMargin:" + pd.DefaultPageSettings.HardMarginX);
            Console.WriteLine("yHardMargin:" + pd.DefaultPageSettings.HardMarginY);

            pd.PrintPage += PrintPage;

            try
            {
                pd.Print();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Print exception:" + ex.Message);
            }

        }
        private static void PrintPage(object o, PrintPageEventArgs e)
        {
            Image img = Image.FromFile(@"D:\Demo\picWorldCup\005.png");
            //int width = int.Parse((img.Height * 0.7f).ToString());
            int width = (int)(img.Height * 0.7f);
            //e.Graphics.DrawImage(img, new Rectangle(0, 0, 70, 100), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);
            //e.Graphics.DrawImage(img, new Rectangle(0,0, 350, 500),new Rectangle(0,0, width, img.Height),GraphicsUnit.Pixel);
            //e.Graphics.DrawImage(img, new Rectangle(0, 0, 350, 500), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);//手机拍照

            //e.Graphics.DrawImage(img, new Rectangle(0, 0, 355, 500), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);//AR拍照
            e.Graphics.DrawImage(img, new Rectangle(0, 0, 355, 500), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);//通用
            //e.Graphics.DrawImage(img, new Rectangle(0,0, 89, 127),new Rectangle(0,0, width, img.Height),GraphicsUnit.Millimeter);
            //e.Graphics.DrawImage(img, 0,0,width,img.Height);
            Console.WriteLine("W:{0}  H:{1}", img.Width, img.Height);
        }

    }

 

 

其中:DefaultPageSettings参数不用代码设置,有的如页边距无法设置(只读的),此时只要在设置-打印机-设置打印机的首选项参数即可

C# 打印机

标签:microsoft   mil   process   ntb   obj   mes   setting   size   设置   

原文地址:https://www.cnblogs.com/llstart-new0201/p/9486246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!