如何在c#中打印itf条形码

时间:2009-08-10 08:34:57

标签: c# windows printing gdi barcode

我需要使用GDI +打印并交错5个条形码中的2个(18位无校验和)。我使用Web上的一些代码为代码128条形码做了这个。条码扫描器unfortunatley不读取代码128所以我不得不求助于ITF条形码。

谢谢, 肯

2 个答案:

答案 0 :(得分:5)

class I2of5
{
    private static string[] patterns =
    {
        "NNWWN",//0
        "WNNNW",//1
        "NWNNW",//2
        "WWNNN",//3
        "NNWNW",//4
        "WNWNN",//5
        "NWWNN",//6
        "NNNWW",//7
        "WNNWN",//8
        "NWNWN"//9
    };

    public I2of5()
    {

    }
    public static Image MakeBarcodeImage(string barcodeNumber)
    {
        int width = 420;
        int height = 60;

        string barcodeString = "NnNn";//start pattern
        int barcodeLength = barcodeNumber.Length;
        for(int i =0 ; i < barcodeLength; i++)
        {
            int firstNumber = int.Parse(barcodeNumber[i].ToString());
            int secondNumber = int.Parse(barcodeNumber[i + 1].ToString());

            string firstPattern = patterns[firstNumber].ToUpper();
            string secondPattern = patterns[secondNumber].ToLower();

            barcodeString += String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", firstPattern[0], secondPattern[0],
                firstPattern[1] , secondPattern[1] ,
                firstPattern[2] , secondPattern[2] , 
                firstPattern[3] , secondPattern[3] , 
                firstPattern[4] , secondPattern[4]);

            i++;
        }
        barcodeString +="WnN";//stop pattern

        Image barcodeImage = new System.Drawing.Bitmap(width, height);
        using (Graphics gr = Graphics.FromImage(barcodeImage))
        {

            // set to white so we don't have to fill the spaces with white
            gr.FillRectangle(Brushes.White, 0, 0, width, height);

            int cursor = 0;

            for (int codeidx = 0; codeidx < barcodeString.Length; codeidx++)
            {
                char code = barcodeString[codeidx];

                int BarWeight = 1;
                int barwidth = ((code == 'N') || (code == 'n'))?2 * BarWeight: 6 * BarWeight;

                if((code == 'N') || (code == 'W'))
                {
                    gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth , height);
                }

                // note that we never need to draw the space, since we 
                // initialized the graphics to all white

                // advance cursor beyond this pair
                cursor += barwidth;
            }



        }
        return barcodeImage;
    }

答案 1 :(得分:1)

我不知道GDI +,但你可以用iTextSharp(开源)来做到这一点。这里有一个例子:http://itextsharp.sourceforge.net/examples/Chap0907.cs

这里有一个解释:http://itextsharp.sourceforge.net/tutorial/ch09.html#barcode