My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://perevoznyk.wordpress.com
and update your bookmarks.

Thursday, January 20, 2011

Code 39 barcode in C#

Some time ago I published the EAN 13 barcode generator source code. Now in addition to it made I simple Code 39 generator.


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Karna.Barcode
{
    public class Code39
    {
        static readonly string star = "*";
        private Image image;
        private string barcodeId;
        private int width;
        private int height;


        public Code39(Image image)
        {
            this.image = image;
            this.width = 210;
            this.height = 146;
        }

        public int Width
        {
            get { return this.width; }
            set { this.width = value; }
        }

        public int Height
        {
            get { return this.height; }
            set { this.height = value; }
        }

        public string BarcodeId
        {
            get { return barcodeId; }
            set
            {
                if (value == null)
                    return;

                if (!value.StartsWith(Code39.star))
                {
                    value = Code39.star + value;
                }

                if (!value.EndsWith(Code39.star))
                {
                    value = value + Code39.star;
                }

                barcodeId = value;

                char encChar;

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < value.Length; i++)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append('0');
                    }

                    encChar = value[i];

                    switch (encChar)
                    #region Char Table
                    {
                        case '0': sb.Append("101001101101");
                            break;
                        case '1': sb.Append("110100101011");
                            break;
                        case '2': sb.Append("101100101011");
                            break;
                        case '3': sb.Append("110110010101");
                            break;
                        case '4': sb.Append("101001101011");
                            break;
                        case '5': sb.Append("110100110101");
                            break;
                        case '6': sb.Append("101100110101");
                            break;
                        case '7': sb.Append("101001011011");
                            break;
                        case '8': sb.Append("110100101101");
                            break;
                        case '9': sb.Append("101100101101");
                            break;
                        case 'A': sb.Append("110101001011");
                            break;
                        case 'B': sb.Append("101101001011");
                            break;
                        case 'C': sb.Append("110110100101");
                            break;
                        case 'D': sb.Append("101011001011");
                            break;
                        case 'E': sb.Append("110101100101");
                            break;
                        case 'F': sb.Append("101101100101");
                            break;
                        case 'G': sb.Append("101010011011");
                            break;
                        case 'H': sb.Append("110101001101");
                            break;
                        case 'I': sb.Append("101101001101");
                            break;
                        case 'J': sb.Append("101011001101");
                            break;
                        case 'K': sb.Append("110101010011");
                            break;
                        case 'L': sb.Append("101101010011");
                            break;
                        case 'M': sb.Append("110110101001");
                            break;
                        case 'N': sb.Append("101011010011");
                            break;
                        case 'O': sb.Append("110101101001");
                            break;
                        case 'P': sb.Append("101101101001");
                            break;
                        case 'Q': sb.Append("101010110011");
                            break;
                        case 'R': sb.Append("110101011001");
                            break;
                        case 'S': sb.Append("101101011001");
                            break;
                        case 'T': sb.Append("101011011001");
                            break;
                        case 'U': sb.Append("110010101011");
                            break;
                        case 'V': sb.Append("100110101011");
                            break;
                        case 'W': sb.Append("110011010101");
                            break;
                        case 'X': sb.Append("100101101011");
                            break;
                        case 'Y': sb.Append("110010110101");
                            break;
                        case 'Z': sb.Append("100110110101");
                            break;
                        case '-': sb.Append("100101011011");
                            break;
                        case '.': sb.Append("110010101101");
                            break;
                        case ' ': sb.Append("100110101101");
                            break;
                        case '$': sb.Append("100100100101");
                            break;
                        case '/': sb.Append("100100101001");
                            break;
                        case '+': sb.Append("100101001001");
                            break;
                        case '%': sb.Append("101001001001");
                            break;
                        case '*': sb.Append("100101101101");
                            break;
                        default:
                            break;
                    }
                    #endregion
                }
                
                string drawString = sb.ToString();

                Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                Graphics canvas = Graphics.FromImage(bitmap);
                canvas.Clear(Color.White);

                canvas.SmoothingMode = SmoothingMode.HighQuality;
                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                canvas.CompositingQuality = CompositingQuality.HighQuality;

                int length = drawString.Length;
                int start = 0;
                int final = 0;
                int count = 0;

                while (start < length)
                {

                    while (final < length)
                    {
                        if (drawString[final] == '1')
                        {
                            final++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    count = final - start;

                    if (count > 0)
                    {
                        RectangleF bounds = new RectangleF(0, 0, bitmap.Width, bitmap.Height);
                        RectangleF rect = bounds;
                        rect.X += bounds.Width * start / length;
                        rect.Width = bounds.Width * count / length;
                        canvas.FillRectangle(Brushes.Black, rect);
                    }

                    while (final < length)
                    {
                        if (drawString[final] == '0')
                        {
                            final++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    start = final;
                }

                if (image != null)
                {
                    Graphics g = Graphics.FromImage(image);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.DrawImage(bitmap, 0, 0, image.Width, image.Height);
                    g.Dispose();
                }

                canvas.Dispose();
            }
        }
    }
}