Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 1:41
Akshay_8817-Jul-12 1:41 
GeneralRe: How to find API's to generate barcodes in C# Pin
Dave Kreskowiak17-Jul-12 2:11
mveDave Kreskowiak17-Jul-12 2:11 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 2:19
Akshay_8817-Jul-12 2:19 
QuestionRe: How to find API's to generate barcodes in C# Pin
Richard MacCutchan17-Jul-12 2:44
mveRichard MacCutchan17-Jul-12 2:44 
AnswerRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 19:24
Akshay_8817-Jul-12 19:24 
GeneralRe: How to find API's to generate barcodes in C# Pin
Richard MacCutchan17-Jul-12 21:29
mveRichard MacCutchan17-Jul-12 21:29 
AnswerRe: How to find API's to generate barcodes in C# Pin
Luc Pattyn17-Jul-12 2:52
sitebuilderLuc Pattyn17-Jul-12 2:52 
AnswerRe: How to find API's to generate barcodes in C# Pin
DaveyM6920-Jul-12 8:05
professionalDaveyM6920-Jul-12 8:05 
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.
C#
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>()
        {
            // b or w = 0, B or W = 1
            // bwbWBwBwb
            {'0', 0x0034},
            // BwbWbwbwB
            {'1', 0x0121},
            // bwBWbwbwB
            {'2', 0x0061},
            // BwBWbwbwb
            {'3', 0x0160},
            // bwbWBwbwB
            {'4', 0x0031},
            // BwbWBwbwb
            {'5', 0x0130},
            // bwBWBwbwb
            {'6', 0x0070},
            // bwbWbwBwB
            {'7', 0x0025},
            // BwbWbwBwb
            {'8', 0x0124},
            // bwBWbwBwb
            {'9', 0x0064},

            // ToDo: Add A-Z and special characters

            // bWbwBwBwb
            {'*', 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;
                // narrows + wides + separators + 1 pixel
                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; // separator
                        DrawCharacter(c, g, brush, ref x, narrowWidth, height, ratio);
                    }
                    x += narrowWidth; // separator
                    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) // bar
                    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;
        }
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



QuestionC# files missing .cs file Pin
Member 906602516-Jul-12 20:33
Member 906602516-Jul-12 20:33 
GeneralRe: C# files missing .cs file Pin
Sandeep Mewara16-Jul-12 21:11
mveSandeep Mewara16-Jul-12 21:11 
GeneralRe: C# files missing .cs file Pin
Member 906602516-Jul-12 22:44
Member 906602516-Jul-12 22:44 
AnswerRe: C# files missing .cs file Pin
Sandeep Mewara16-Jul-12 23:39
mveSandeep Mewara16-Jul-12 23:39 
GeneralRe: C# files missing .cs file Pin
Member 906602517-Jul-12 0:49
Member 906602517-Jul-12 0:49 
GeneralRe: C# files missing .cs file Pin
Pete O'Hanlon17-Jul-12 1:19
mvePete O'Hanlon17-Jul-12 1:19 
GeneralRe: C# files missing .cs file Pin
Member 906602517-Jul-12 3:51
Member 906602517-Jul-12 3:51 
GeneralRe: C# files missing .cs file Pin
Pete O'Hanlon17-Jul-12 3:52
mvePete O'Hanlon17-Jul-12 3:52 
GeneralRe: C# files missing .cs file Pin
Member 906602517-Jul-12 20:07
Member 906602517-Jul-12 20:07 
GeneralRe: C# files missing .cs file Pin
Member 906602517-Jul-12 21:54
Member 906602517-Jul-12 21:54 
GeneralRe: C# files missing .cs file Pin
Pete O'Hanlon17-Jul-12 22:58
mvePete O'Hanlon17-Jul-12 22:58 
GeneralRe: C# files missing .cs file Pin
Member 906602518-Jul-12 0:47
Member 906602518-Jul-12 0:47 
GeneralRe: C# files missing .cs file Pin
Pete O'Hanlon18-Jul-12 1:30
mvePete O'Hanlon18-Jul-12 1:30 
GeneralRe: C# files missing .cs file Pin
Member 906602518-Jul-12 2:02
Member 906602518-Jul-12 2:02 
QuestionString to Object Instance Problem Pin
atoi_powered16-Jul-12 14:04
atoi_powered16-Jul-12 14:04 
AnswerRe: String to Object Instance Problem Pin
Wes Aday16-Jul-12 14:45
professionalWes Aday16-Jul-12 14:45 
GeneralRe: String to Object Instance Problem Pin
atoi_powered17-Jul-12 0:40
atoi_powered17-Jul-12 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.