This is a class I wrote some time ago for Code39 - I only needed 0 thru 9 (and * of course) so the others aren't in the dictionary bey easy enough to add.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
namespace Barcodes.Code39
{
[Serializable]
public class Barcode : IEquatable<Barcode>
{
public static readonly Color DefaultBarColor = Color.Black;
public const int DefaultNarrowWidth = MinNarrowWidth;
public const int MinNarrowWidth = 1;
public const int MinHeight = 1;
private const int NormalRatio = 2;
private const int LargeRatio = 3;
internal static readonly char StartStopCharacter = '*';
public static readonly IList<char> ValidCharacters =
new ReadOnlyCollection<char>(new char[]{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'-', '.', ' ',
'$', '/', '+', '%'
});
private static readonly Dictionary<char, short> CharacterValuesDictionary =
new Dictionary<char, short>()
{
{'0', 0x0034},
{'1', 0x0121},
{'2', 0x0061},
{'3', 0x0160},
{'4', 0x0031},
{'5', 0x0130},
{'6', 0x0070},
{'7', 0x0025},
{'8', 0x0124},
{'9', 0x0064},
{'*', 0x0094}
};
private string value;
public Barcode(string value)
{
if (value != null)
{
value = value.ToUpperInvariant();
StringBuilder stringBuilder = new StringBuilder(value.Length);
foreach (char c in value)
if (ValidCharacters.Contains(c))
stringBuilder.Append(c);
if (stringBuilder.Length > 0)
this.value = stringBuilder.ToString();
}
}
public static implicit operator string(Barcode barcode)
{
return barcode.value;
}
public static explicit operator Barcode(string value)
{
return new Barcode(value);
}
public static bool operator ==(Barcode first, Barcode other)
{
if (object.ReferenceEquals(first, other))
return true;
if (object.ReferenceEquals(null, first) || object.ReferenceEquals(null, other))
return false;
return first.value == other.value;
}
public static bool operator !=(Barcode first, Barcode other)
{
return !(first == other);
}
public Image Draw(int height)
{
return Draw(DefaultBarColor, DefaultNarrowWidth, height, false);
}
public Image Draw(Color barColor, int height)
{
return Draw(barColor, DefaultNarrowWidth, height, false);
}
public Image Draw(int narrowWidth, int height)
{
return Draw(DefaultBarColor, narrowWidth, height, false);
}
public Image Draw(int height, bool largeRatio)
{
return Draw(DefaultBarColor, DefaultNarrowWidth, height, largeRatio);
}
public Image Draw(Color barColor, int height, bool largeRatio)
{
return Draw(barColor, DefaultNarrowWidth, height, largeRatio);
}
public Image Draw(int narrowWidth, int height, bool largeRatio)
{
return Draw(DefaultBarColor, narrowWidth, height, largeRatio);
}
public Image Draw(Color barColor, int narrowWidth, int height, bool largeRatio)
{
if (narrowWidth < MinNarrowWidth)
narrowWidth = MinNarrowWidth;
if (height < MinHeight)
height = MinHeight;
if (value != null)
{
int ratio = largeRatio ? LargeRatio : NormalRatio;
int charactersCount = value.Length + 2;
int width =
(charactersCount * (narrowWidth * 6)) +
(charactersCount * ((narrowWidth * ratio)* 3)) +
((charactersCount-1) * narrowWidth) +
1;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
bitmap.MakeTransparent(Color.Transparent);
using (Graphics g = Graphics.FromImage(bitmap))
using (Brush brush = new SolidBrush(barColor))
{
g.Clear(Color.Transparent);
int x = 0;
DrawCharacter(StartStopCharacter, g, brush, ref x, narrowWidth, height, ratio);
foreach (char c in value)
{
x += narrowWidth;
DrawCharacter(c, g, brush, ref x, narrowWidth, height, ratio);
}
x += narrowWidth;
DrawCharacter(StartStopCharacter, g, brush, ref x, narrowWidth, height, ratio);
}
return bitmap;
}
else
return null;
}
private void DrawCharacter(char c, Graphics g, Brush brush, ref int x, int narrowWidth, int height, int ratio)
{
short characterValue = CharacterValuesDictionary[c];
for (int bitIndex = 8; bitIndex >= 0; bitIndex--)
{
int bit = (characterValue >> bitIndex) & 1;
int width = bit == 1 ? narrowWidth * ratio : narrowWidth;
if ((bitIndex % 2) == 0)
g.FillRectangle(brush, x, 0, width, height);
x += width;
}
}
public override bool Equals(object obj)
{
if (obj is Barcode)
return Equals(obj as Barcode);
return base.Equals(obj);
}
public bool Equals(Barcode other)
{
return this == other;
}
public override int GetHashCode()
{
if (value == null)
return 0;
return value.GetHashCode();
}
public override string ToString()
{
return value;
}
}
}
|