Never label anything 'Miscellaneous.'

In which we apply barcode labels to the world.

I am currently working on a project that involves process tracking and automation. The story in a nutshell is the current process is too tedious and cumbersome and results in a great deal of extra work to compile and interpret the metrics. What better way to track a thing, physical or otherwise*, than to affix a barcode to it? None! And did I mention, barcodes are cool?

When making the prototype, I found a plethora of sites that allow users to make barcode labels one at a time with garish watermarks. Perhaps this is workable if one needs but a few barcodes, but it was unsuitable for my needs. Thus I set out to make my own bardcode label maker.

As it turns out, a gentleman by the name of Matthew Welch has already done most of the heavy lifting. Mr. Welch has created and released a barcode font[1] for 3 of 9 (code39) barcodes. What's more, he has done so free of charge. With Mr. Welch's font in hand, I created a simple app to generate barcodes.

BarcodeGen source on bitbucket or executable (10.6KB)

Both the repo on bitbucket and the zip contain the required barcode font that you must install before using the app. Simply open the font file and click the Install button. There are two versions of the font, the standard one and the extended one. The app uses the standard one, but you can modify this line

private static readonly Font BarcodeFont = new Font("Free 3 of 9", 36);
where the string is the font name and the number is the font size (you can make the label larger or smaller by adjusting the font size) to use the extended font.

You can also change the font used for the caption by modifying this line

private static readonly Font CaptionFont = new Font("Consolas", 10);
The generation of each barcode happens in DrawBarcodeLabel. Essentially we create a graphics object to measure how much space we need to render the barcode and the caption.  Whichever is wider is used as the width of the label. To center the smaller of the two strings in the label, we find the center of the larger one, then move to the left half the width of the smaller one. Rinse and repeat.
private static Image DrawBarcodeLabel(string barcode, string caption, Font font, Font captionFont, Color textColor, Color backColor)
            // get a graphics object
            var img = new Bitmap(1, 1);
            var drawing = Graphics.FromImage(img);

            // measure the barcode size
            var textSizeBarcode = drawing.MeasureString(barcode, font);
            // measure the caption size
            var textSizeCaption = drawing.MeasureString(caption, captionFont);

            // calculate the total image size
            var width = textSizeBarcode.Width > textSizeCaption.Width ? textSizeBarcode.Width : textSizeCaption.Width;
            var height = textSizeBarcode.Height + textSizeCaption.Height;

            // release graphics object

            // now let's make the label
            img = new Bitmap((int)width, (int)height);
            drawing = Graphics.FromImage(img);

            // set the background
            // and the text color
            Brush textBrush = new SolidBrush(textColor);

            var codeX = 0f;
            var captionX = 0f;
            // if the caption is smaller than the barcode we center the caption
            // otherwise we need to center the barcode
            if (textSizeCaption.Width < width)
                // center the caption below the barcode
                captionX = width / 2 - textSizeCaption.Width / 2;
                // center the barcode above the caption
                codeX = width / 2 - textSizeBarcode.Width / 2;

            // draw the barcode
            drawing.DrawString(barcode, font, textBrush, codeX, 0);
            // draw the caption
            drawing.DrawString(caption, captionFont, textBrush, captionX, textSizeBarcode.Height);



            return img;

The application can be used either interactively or you can pass it a file on the command line with contents in the format BARCODE[,CAPTION]

In interactive mode, whatever text is supplied is used to generate the barcode and the caption. The command line mode offers greater flexibility in that the caption can be different from the barcode or omitted altogether. If you make use of this app, I would appreciate hearing from you.

[*] Figuring out how to attache a label to the intangible is left as an exercise for the reader.