技术思绪摘录旅行
经常会遇到批量打印的场景,比如外卖单子、核酸检测条码等,都是定一个规则之后,批量生产然后批量打印出来,我这里不介绍业务实现,只记录一下打印机的调用

1、获取默认打印机

PrintDocument fPrintDocument = new PrintDocument();
string defaultPrinter = fPrintDocument.PrinterSettings.PrinterName;

2、获取所有可用打印机

for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                string tmp = PrinterSettings.InstalledPrinters[i];
                if (tmp == defaultPrinter)
                {
                    tmp = tmp + "(默认)";
                }
                lb_devices.Items.Add(tmp);
            }

3、设置指定打印机为默认打印机

[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机

SetDefaultPrinter("打印机名");

4、直接打印

1、添加一个控件System.Drawing.Printing.PrintDocument
2、添加printDocument1_PrintPage事件(GetBitmap()方法是自定义的GDI画打印图片的)
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.HasMorePages = false;
            e.Graphics.Clear(Color.White);

            e.Graphics.DrawImage(GetBitmap(), 0, 0, txt_bg_w.Text.ToInt(), txt_bg_h.Text.ToInt());
        }
3、调用打印
 StandardPrintController spc = new StandardPrintController();
 printDocument1.PrintController = spc;
 printDocument1.Print();

5、GDI中文字垂直排版

Bitmap ob = new Bitmap(500, 500);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.White);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;

                //画刷颜色
                SolidBrush txtBrush = new SolidBrush(ColorTranslator.FromHtml("#000000"));
                //格式居中
                StringFormat txtFromat = new StringFormat();
                txtFromat.Alignment = StringAlignment.Center;
                string remark = txt_top_ramrk.Text;
                if (remark.Length > 10)
                {
                    remark = remark.Substring(0, 10);
                }

g.RotateTransform(270);
g.DrawString("垂直显示的文字", new Font("微软雅黑", 9), txtBrush, new Point(-55, 0), txtFromat);

g?.Dispose();
ob.Save(@"imgs/img.jpg",ImageFormat.Jpeg);


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫