barcodelib打印条码C#例子

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/luojianfeng/article/details/77002542

先引用barcodelib控件



using System.Threading;
using System.Drawing.Printing;


private void button1_Click(object sender, EventArgs e)

        {
            DialogResult result = MessageBox.Show("你确定要打印条码一?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);


            if (result == DialogResult.OK)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                //打印次数
                printDocument1.PrinterSettings.Copies = Convert.ToInt16(txt_print.Text.Trim());
                //换成多线程方式
                if (thre == null)
                {
                    thre = new Thread(this.printDocument1.Print);
                    thre.Start();
                }
                if (thre.ThreadState == ThreadState.Stopped)
                {
                    thre.Abort();
                    GC.Collect();
                    thre = new Thread(this.printDocument1.Print);
                    thre.Start();
                }
            }

        }


private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            ////打印内容 为 局部的 this.groupBox1
            //Bitmap _NewBitmap = new Bitmap(pane_Barcode.Width, pane_Barcode.Height);
            //pane_Barcode.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);


            //Bitmap _NewBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            ////pane_Barcode.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);


                System.Drawing.Image image;
                int width = 100, height = 55;
                int width2 = 370, height2 = 50;


                //注意:登陆的时候有把服务器的这2个文件拷贝到本地
                fileSavePath0_ = System.IO.Path.GetTempPath() + "BarcodePattern.jpg";
                fileSavePath1_ = System.IO.Path.GetTempPath() + "BarcodePattern2.jpg";


                DateTime dt = DateTime.Now;


                fileSavePath = System.IO.Path.GetTempPath() + string.Format("{0:yyyyMMddHHmmssffff}", dt) + ".jpg";
                if (File.Exists(fileSavePath))
                {
                    fileSavePath = System.IO.Path.GetTempPath() + string.Format("{0:yyyyMMddHHmmssffff}", dt) + "1.jpg";
                }
                File.Copy(fileSavePath0_, fileSavePath);


                DateTime dt2 = DateTime.Now;


                fileSavePath2 = System.IO.Path.GetTempPath() + string.Format("{0:yyyyMMddHHmmssffff}", dt2) + ".jpg";
                if (File.Exists(fileSavePath2))
                {
                    fileSavePath2 = System.IO.Path.GetTempPath() + string.Format("{0:yyyyMMddHHmmssffff}", dt2) + "2.jpg";
                }
                System.IO.File.Copy(fileSavePath1_, fileSavePath2);


                GetBarcode(height, width, BarcodeLib.TYPE.CODE128, txt_qty.Text + "  ", out image, fileSavePath2, 1);
                pictureBox6.Image = GetFile(fileSavePath2);


                GetBarcode(height2, width2, BarcodeLib.TYPE.CODE128, txt_barcode.Text, out image, fileSavePath, 2);
                pictureBox7.Image = GetFile(fileSavePath);


                //---------------------------------------------------------






                Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
                panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
                e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);


                e.Graphics.DrawString("CHIAPHUA COMPONENTS LTD", new Font(new FontFamily("宋体"), 14), System.Drawing.Brushes.Blue, 70, 12);


                e.Graphics.DrawString("MODEL :", new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 8, 40);
                e.Graphics.DrawString(txt_part.Text, new Font(new FontFamily("Arial"), 14, FontStyle.Bold), System.Drawing.Brushes.Blue, 100, 38);


                e.Graphics.DrawString("CUST PART:", new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 8, 64);
                e.Graphics.DrawString(txt_cpart.Text, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 100, 64);


                e.Graphics.DrawString("P/O#:", new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 8, 90);
                e.Graphics.DrawString(txt_cpo.Text, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 100, 90);


                e.Graphics.DrawString("Date Code:" + txt_Dcode.Text, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 240, 90);


                e.Graphics.DrawString("QTY:", new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 8, 118);
                e.Graphics.DrawString(txt_qty.Text + "EA", new Font(new FontFamily("Arial"), 16, FontStyle.Bold), System.Drawing.Brushes.Blue, 100, 114);


                e.Graphics.DrawString("DESC: ", new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 8, 144);
                e.Graphics.DrawString(txt_des.Text, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Blue, 100, 144);


                string kkk = "";
                kkk = kkk + "RoHS ";


                e.Graphics.DrawString(kkk, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Blue, 290, 144);
            }
}


public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl, int wz)
        {
            try
            {
                image = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                b.BackColor = System.Drawing.Color.White;//图片背景颜色
                b.ForeColor = System.Drawing.Color.Black;//条码颜色
                b.IncludeLabel = true;
                if (wz == 1)
                {
                    b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                }
                if (wz == 2)
                {
                    b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                }
                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置
                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式
                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置
                b.LabelFont = font;
                b.Height = height;//图片高度设置(px单位)
                b.Width = width;//图片宽度设置(px单位)


                image = b.Encode(type, code);//生成图片
                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }


        private Image GetFile(string path)
        {
            MemoryStream stream = ReadFile(path);
            return stream == null ? null : Image.FromStream(stream);
        }


        private MemoryStream ReadFile(string path)
        {
            if (!File.Exists(path))
                return null;


            using (FileStream file = new FileStream(path, FileMode.Open))
            {
                byte[] b = new byte[file.Length];
                file.Read(b, 0, b.Length);


                MemoryStream stream = new MemoryStream(b);
                return stream;
            }
        }

展开阅读全文

没有更多推荐了,返回首页