Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

Barcode UPC-A

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
7 Jul 2007CPOL1 min read 335.2K   4K   66  
UPC-A C# class that will generate UPC-A codes
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace UPCA
{
  class cUPCA
  {
    private Bitmap newBitmap;
    private Graphics g;
    private int barCodeHeight = 80;
    private int placeMarker = 0;
    private int imageWidth = 0;
    private float imageScale = 1;
    private string UPCABegin = "0000000000000101";
    private string[] UPCALeft = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
    private string UPCAMiddle = "01010";
    private string[] UPCARight = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
    private string UPCAEnd = "1010000000000000";

    public Image CreateBarCode(string txt, int scale)
    {
      Image img = null;
      imageWidth = 120;
      imageScale = scale;
      imageWidth = System.Convert.ToInt32(imageWidth * imageScale);
      int imageHeightHolder = System.Convert.ToInt32(barCodeHeight * imageScale);
      string incomingString = txt.ToUpper();
      if(incomingString.Length == 0)
      {
        return img;
      }
      int numberOfChars = incomingString.Length;
      newBitmap = new Bitmap((imageWidth), imageHeightHolder, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
      g = Graphics.FromImage(newBitmap);
      g.ScaleTransform(imageScale, imageScale);
      Rectangle newRec = new Rectangle(0, 0, (imageWidth), imageHeightHolder);
      g.FillRectangle(new SolidBrush(Color.White), newRec);
      placeMarker = 0;
      txt = txt.Substring(0, 11) + GetCheckSum(txt).ToString();
      int wholeSet = 0;
      for(wholeSet = 1; wholeSet <= System.Convert.ToInt32(incomingString.Length); wholeSet++)
      {
        int currentASCII = (int)Convert.ToChar((incomingString.Substring(wholeSet - 1, 1))) - 48;
        string currentLetter = "";
        if(wholeSet == 1)
        {
          DrawSet(UPCABegin, placeMarker, 0);
          DrawSet(UPCALeft[currentASCII], placeMarker, 0);
        }
        else if(wholeSet <= 5)
        {
          DrawSet(UPCALeft[currentASCII], placeMarker, 6);
        }
        else if(wholeSet == 6)
        {
          DrawSet(UPCALeft[currentASCII], placeMarker, 6);
          DrawSet(UPCAMiddle, placeMarker, 0);
        }
        else if(wholeSet <= 11)
        {
          DrawSet(UPCARight[currentASCII], placeMarker, 6);
        }
        else if(wholeSet == 12)
        {
          DrawSet(UPCARight[currentASCII], placeMarker, 0);
          DrawSet(UPCAEnd, placeMarker, 0);
        }
      }
      
      System.Drawing.Font font = new System.Drawing.Font("Courier New, Bold", 9);
      try
      {
        g.DrawString(txt.Substring(0, 1), font, Brushes.Black, new System.Drawing.PointF(0, 67));
        g.DrawString(txt.Substring(1, 5), font, Brushes.Black, new System.Drawing.PointF(22, 67));
        g.DrawString(txt.Substring(6, 5), font, Brushes.Black, new System.Drawing.PointF(60, 67));
        g.DrawString(txt.Substring(11, 1), font, Brushes.Black, new System.Drawing.PointF(108, 67));
      }
      finally
      {
        font.Dispose();
      }
      img = Image.FromHbitmap(newBitmap.GetHbitmap());
      return img;
    }

    public int GetCheckSum(string barCode)
    {
      string leftSideOfBarCode = barCode.Substring(0, 11);
      int total = 0;
      int currentDigit = 0;
      int i = 0;
      for( i = 0; i <= leftSideOfBarCode.Length - 1; i++)
      {
        currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1));
        if(((i - 1) % 2 == 0))
        {
          total += currentDigit * 1;
        }
        else
        {
          total += currentDigit * 3;
        }
      }
      int iCheckSum = (10 - (total % 10)) % 10;
      return iCheckSum;
    }

    private void DrawSet(string upcCode, int drawLocation, int barHeight)
    {
      int[] currentLetterArray = new int[upcCode.Length];
      placeMarker += upcCode.Length;
      barHeight = barCodeHeight - barHeight;
      int i = 0;
      for(i = 0; i <= upcCode.Length - 1; i++)
      {
        currentLetterArray[i] = Convert.ToInt16(upcCode.Substring(i, 1));
      }
      for(i = 0; i <= upcCode.Length - 1; i++)
      {
        if(currentLetterArray[i] == 0)
        {
          g.DrawLine(Pens.White, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
        }
        else if(currentLetterArray[i] == 1)
        {
          g.DrawLine(Pens.Black, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
        }
      }
    }

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions