Generador de código de barras

Como dice en la introducción hay ocasiones que se necesitan vender productos que no tienen código de barras, como cigarros sueltos,vasos de plastico, velas, medio kilo huevo,etc, para ingresar esas ventas a nuestro punto de venta es necesario crear un generador de código de barras para poder capturar su precio, y la venta del producto.

Creamos un Windows Form y lo nombramos CodigoBarras.cs y al formulario lo nombramos frmCodigoBarras.
Agregamos un MenuStrip lo nombramos Opciones agregamos un opción y la nombramos Menú
punto de venta
Agregamos un Label y en texto ponemos Generador de Código de Barras
punto de venta
Nos vamos al Cuadro de Herramientas y Selecionamos un Panel. y lo nombramos pnlCodigoBarras
punto de venta
punto de venta
Creamos un Label y en texto ponemos Texto para código de barras:
punto de venta
Ahora creamos un TextBox y lo nombramos txtCodigo
punto de venta
Seleccionamos un Button en en el texto ponemos Generar Código de Barras y lo nombramos btnGenerarCodigo.
punto de venta
Copiamos y pegamos el Button y en texto agregamos Guardar Código de Barras y lo nombramos btnGuardarCodigo
. punto de venta
Copiamos y pegamos un Button y en texto colocamos Imprimir Codigo de Barras y lo nombramos btnImprimirCodigo
punto de venta
Lo primero que vamos a realizar es importar la librería barcodelib para generar el código de barras nos vamos al menú Herramientas->Administrador de paquetes Nuget->Administrar paquetes Nuget para la solución...
punto de venta
buscamos barcodelib seleccionamos la librería.tildamos en el Proyecto y GatoAbarrotero y pulsamos instalar
punto de venta
Presionamos Aceptar
punto de venta
Nos vamos al código y agregamos las siguientes líneas que estan escritas en blanco que indican que cuando inicie nuestra ventana no se puedan seleccionar lo botones imprimir y guardar.
 
             using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
             
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
            
        }
    }
}
             
             
             
             
              

Nos vamos al formulario y en el Button Generar Código de Barras hacemos doble clic y escribmos lo siguiente:
 
   using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
        }
 
        private void BtnGenerarCodigo_Click(object sender, EventArgs e)
        {
            BarcodeLib.Barcode codigo = new BarcodeLib.Barcode();
            codigo.IncludeLabel = true;
            pnlCodigoBarras.BackgroundImage = codigo.Encode(BarcodeLib.TYPE.CODE128, txtCodigo.Text, Color.Black, Color.White, 400, 100);
            btnGuardarCodigo.Enabled = true;
            btnImprimirCodigo.Enabled = true;
        }
        
    }
}        
           
           
           
  

Ahora vamos con la parte de guardar código de barras nos vamos al Button Guardar Código de Barras y hacemos doble click:
         
    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Drawing.Imaging;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
        }

        private void BtnGenerarCodigo_Click(object sender, EventArgs e)
        {
            BarcodeLib.Barcode codigo = new BarcodeLib.Barcode();
            codigo.IncludeLabel = true;
            pnlCodigoBarras.BackgroundImage = codigo.Encode(BarcodeLib.TYPE.CODE128, txtCodigo.Text, Color.Black, Color.White, 400, 100);
            btnGuardarCodigo.Enabled = true;
            btnImprimirCodigo.Enabled = true;
        }
        
        private void BtnGuardarCodigo_Click(object sender, EventArgs e)
        {
            Image imgFinal = (Image)pnlCodigoBarras.BackgroundImage.Clone();
            SaveFileDialog dialogoGuardar = new SaveFileDialog();
            dialogoGuardar.AddExtension = true;
            dialogoGuardar.Filter = "Image PNG (*.png)|*.png";
            dialogoGuardar.ShowDialog();
            if (!string.IsNullOrEmpty(dialogoGuardar.FileName))
            {
                imgFinal.Save(dialogoGuardar.FileName, ImageFormat.Png);
            }
            imgFinal.Dispose();
        }
        
    }
}
           
           
           
     

Para imprimir el código de barras nos vamos a cuadro de herramientas y selecionamos un PrintDocument
punto de venta
punto de venta
Hacemos doble click sobre printDocument1 y escrbimos esto:
 
   using System;  
     using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
        }

        private void BtnGenerarCodigo_Click(object sender, EventArgs e)
        {
            BarcodeLib.Barcode codigo = new BarcodeLib.Barcode();
            codigo.IncludeLabel = true;
            pnlCodigoBarras.BackgroundImage = codigo.Encode(BarcodeLib.TYPE.CODE128, txtCodigo.Text, Color.Black, Color.White, 400, 100);
            btnGuardarCodigo.Enabled = true;
            btnImprimirCodigo.Enabled = true;
        }

        private void BtnGuardarCodigo_Click(object sender, EventArgs e)
        {
            Image imgFinal = (Image)pnlCodigoBarras.BackgroundImage.Clone();
            SaveFileDialog dialogoGuardar = new SaveFileDialog();
            dialogoGuardar.AddExtension = true;
            dialogoGuardar.Filter = "Image PNG (*.png)|*.png";
            dialogoGuardar.ShowDialog();
            if (!string.IsNullOrEmpty(dialogoGuardar.FileName))
            {
                imgFinal.Save(dialogoGuardar.FileName, ImageFormat.Png);
            }
            imgFinal.Dispose();
        }
         
        private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height);
            pnlCodigoBarras.DrawToBitmap(bm, new Rectangle(0, 0, this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }
        
    }
}
      
     
     
     

Realizamos doble click sobre el Button Imprimir Código de Barras y escribmos la siguientes líneas:
  
     
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
        }

        private void BtnGenerarCodigo_Click(object sender, EventArgs e)
        {
            BarcodeLib.Barcode codigo = new BarcodeLib.Barcode();
            codigo.IncludeLabel = true;
            pnlCodigoBarras.BackgroundImage = codigo.Encode(BarcodeLib.TYPE.CODE128, txtCodigo.Text, Color.Black, Color.White, 400, 100);
            btnGuardarCodigo.Enabled = true;
            btnImprimirCodigo.Enabled = true;
        }

        private void BtnGuardarCodigo_Click(object sender, EventArgs e)
        {
            Image imgFinal = (Image)pnlCodigoBarras.BackgroundImage.Clone();
            SaveFileDialog dialogoGuardar = new SaveFileDialog();
            dialogoGuardar.AddExtension = true;
            dialogoGuardar.Filter = "Image PNG (*.png)|*.png";
            dialogoGuardar.ShowDialog();
            if (!string.IsNullOrEmpty(dialogoGuardar.FileName))
            {
                imgFinal.Save(dialogoGuardar.FileName, ImageFormat.Png);
            }
            imgFinal.Dispose();
        }

        private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height);
            pnlCodigoBarras.DrawToBitmap(bm, new Rectangle(0, 0, this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }
       
        private void BtnImprimirCodigo_Click(object sender, EventArgs e)
        {
            printDocument1.Print();

        }
        
    }
}
   
     
     

Nos vamos al menú opciones y hacemos doble click en la opción Menú y escribimos esto:
     using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmCodigoBarras : Form
    {
        public frmCodigoBarras()
        {
            InitializeComponent();
            btnGuardarCodigo.Enabled = false;
            btnImprimirCodigo.Enabled = false;
        }

        private void BtnGenerarCodigo_Click(object sender, EventArgs e)
        {
            BarcodeLib.Barcode codigo = new BarcodeLib.Barcode();
            codigo.IncludeLabel = true;
            pnlCodigoBarras.BackgroundImage = codigo.Encode(BarcodeLib.TYPE.CODE128, txtCodigo.Text, Color.Black, Color.White, 400, 100);
            btnGuardarCodigo.Enabled = true;
            btnImprimirCodigo.Enabled = true;
        }

        private void BtnGuardarCodigo_Click(object sender, EventArgs e)
        {
            Image imgFinal = (Image)pnlCodigoBarras.BackgroundImage.Clone();
            SaveFileDialog dialogoGuardar = new SaveFileDialog();
            dialogoGuardar.AddExtension = true;
            dialogoGuardar.Filter = "Image PNG (*.png)|*.png";
            dialogoGuardar.ShowDialog();
            if (!string.IsNullOrEmpty(dialogoGuardar.FileName))
            {
                imgFinal.Save(dialogoGuardar.FileName, ImageFormat.Png);
            }
            imgFinal.Dispose();
        }

        private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height);
            pnlCodigoBarras.DrawToBitmap(bm, new Rectangle(0, 0, this.pnlCodigoBarras.Width, this.pnlCodigoBarras.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }

        private void BtnImprimirCodigo_Click(object sender, EventArgs e)
        {
            printDocument1.Print();

        }
         
        private void MenúToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmMenu menu = new frmMenu();
            menu.Show();
        }
        
    }
}
     
     
     
      

Ahora nos vamos al formulario Menu.cs y copiamos y pegamos el Button y lo nombramos btnCodigo y en el texto agregamos Código de Barras
punto de venta
Hacemos doble click sobre el Button Código de Barras y escribimos esto:
      using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GatoAbarrotero
{
    public partial class frmMenu : Form
    {
        public frmMenu()
        {
            InitializeComponent();
        }

        private void BtnProductos_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmAgregarProducto agregarProdcuto = new frmAgregarProducto();
            agregarProdcuto.Show();

        }
       
        private void BtnCodigo_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmCodigoBarras codigo = new frmCodigoBarras();
            codigo.Show();
        }
        
    }
}
      
      
      

Probamos el ejemplo:

punto de venta
Escribimos cigarro y presionamos Generar Código de Barras
punto de venta
Ahora guardamos el codigo de barras
punto de venta
Imprimimos el código de barras
punto de venta

Conclusión

Hasta aquí mostramos como generar el código de barras ya que muy sencillo con la librería barcolib lo podemos generar,guardar o imprimir lo que sea mas conveniente.

Referencias

https://www.youtube.com/watch?v=UJm_1BYn9hs