yuanchaost

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

  private void button1_Click(object sender, EventArgs e)
        {
            BuildBarcode();
        }


         void BuildBarcode()
        {
            System.Drawing.Image image;
            int width = 200, height = 100;
            byte[] buffer = GetBarcode(height, width, BarcodeLib.TYPE.CODE128, textBox1.Text, out image);
            try
            {
               // pictureBox1.Image.Save(@"C:\aa.bmp");

                image.Save(@"C:\aa.bmp");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }


        byte[] GetBarcode(int height, int width, BarcodeLib.TYPE type,
                                         string code, out System.Drawing.Image image)
        {
            image = null;
            BarcodeLib.Barcode b = new BarcodeLib.Barcode();
            b.BackColor = System.Drawing.Color.White;
            b.ForeColor = System.Drawing.Color.Black;
            b.IncludeLabel = true;
            b.Width = 50;
            b.Height = 25;
            b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
            b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
            b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);
            b.LabelFont = font;

            b.Height = height;
            b.Width = width;

            image = b.Encode(type, code);
            SaveImage(image, Guid.NewGuid().ToString("N") + ".png");
         

    byte[] buffer = b.GetImageData(BarcodeLib.SaveTypes.PNG);
            return buffer;
        }

        void SaveImage(Image img, string name)
        {
            pictureBox1.Image = img;
        }

posted on 2021-03-15 16:48  yuanchaost  阅读(89)  评论(0编辑  收藏  举报