在网上找到这么段code39的条码代码。执行后只有条码而没有数字,请问要如何修改才能在条码顶部显示制定文字,在条码底部显示条码对应的数字呢?        private Bitmap GetCode39(string strSource)
        {
            int x = 5; //左邊界
            int y = 0; //上邊界
            int WidLength = 2; //粗BarCode長度
            int NarrowLength = 1; //細BarCode長度
            int BarCodeHeight = 24; //BarCode高度
            int intSourceLength = strSource.Length;
            string strEncode = "010010100"; //編碼字串 初值為 起始符號 *  
            string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母  
            string[] Code39 = //Code39的各字母對應碼
            {
                /**//* 0 */ "000110100", 
                /**//* 1 */ "100100001", 
                /**//* 2 */ "001100001", 
                /**//* 3 */ "101100000",
                /**//* 4 */ "000110001", 
                /**//* 5 */ "100110000", 
                /**//* 6 */ "001110000", 
                /**//* 7 */ "000100101",
                /**//* 8 */ "100100100", 
                /**//* 9 */ "001100100", 
                /**//* A */ "100001001", 
                /**//* B */ "001001001",
                /**//* C */ "101001000", 
                /**//* D */ "000011001", 
                /**//* E */ "100011000", 
                /**//* F */ "001011000",
                /**//* G */ "000001101", 
                /**//* H */ "100001100", 
                /**//* I */ "001001100", 
                /**//* J */ "000011100",
                /**//* K */ "100000011", 
                /**//* L */ "001000011", 
                /**//* M */ "101000010", 
                /**//* N */ "000010011",
                /**//* O */ "100010010", 
                /**//* P */ "001010010", 
                /**//* Q */ "000000111", 
                /**//* R */ "100000110",
                /**//* S */ "001000110", 
                /**//* T */ "000010110", 
                /**//* U */ "110000001", 
                /**//* V */ "011000001",
                /**//* W */ "111000000", 
                /**//* X */ "010010001", 
                /**//* Y */ "110010000", 
                /**//* Z */ "011010000",
                /**//* - */ "010000101", 
                /**//* . */ "110000100", 
                /**//*' '*/ "011000100",
                /**//* $ */ "010101000",
                /**//* / */ "010100010", 
                /**//* + */ "010001010", 
                /**//* % */ "000101010", 
                /**//* * */ "010010100" 
            };   
        strSource = strSource.ToUpper();  
        //實作圖片
        Bitmap objBitmap = new Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),BarCodeHeight + (y * 2));  
        Graphics objGraphics = Graphics.FromImage(objBitmap); //宣告GDI+繪圖介面  
        //填上底色
        objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);  
        for (int i = 0; i < intSourceLength; i++) 
        {
            //檢查是否有非法字元
            if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*') 
            {
                objGraphics.DrawString("含有非法字元",SystemFonts.DefaultFont, Brushes.Red, x, y);
                return objBitmap;
            }
                //查表編碼
                strEncode = string.Format("{0}0{1}", strEncode,Code39[AlphaBet.IndexOf(strSource[i])]);
            }  
            strEncode = string.Format("{0}0010010100", strEncode); //補上結束符號 *  
            int intEncodeLength = strEncode.Length; //編碼後長度
            int intBarWidth;  
            for (int i = 0; i < intEncodeLength; i++) //依碼畫出Code39 BarCode
            {
                intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
                objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,x, y, intBarWidth , BarCodeHeight);
                x += intBarWidth;
            }
            return objBitmap; 
        }

解决方案 »

  1.   

    其实就是留出位置来写字符串就是了,参考一下如下的(当然你可以写的更好): protected override void OnClick(EventArgs e)
    {
    base.OnClick(e);
    GetCode39("1234567abcdefg");
    }
    private Bitmap GetCode39(string strSource)
    {
    int x = 5; //左邊界
    int y = 20; //上邊界
    int WidLength = 2; //粗BarCode長度
    int NarrowLength = 1; //細BarCode長度
    int BarCodeHeight = 24; //BarCode高度
    int intSourceLength = strSource.Length;
    string strEncode = "010010100"; //編碼字串 初值為 起始符號 *  
    string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母  
    string[] Code39 = //Code39的各字母對應碼
                {
                    /**//* 0 */ "000110100", 
                    /**//* 1 */ "100100001", 
                    /**//* 2 */ "001100001", 
                    /**//* 3 */ "101100000",
                    /**//* 4 */ "000110001", 
                    /**//* 5 */ "100110000", 
                    /**//* 6 */ "001110000", 
                    /**//* 7 */ "000100101",
                    /**//* 8 */ "100100100", 
                    /**//* 9 */ "001100100", 
                    /**//* A */ "100001001", 
                    /**//* B */ "001001001",
                    /**//* C */ "101001000", 
                    /**//* D */ "000011001", 
                    /**//* E */ "100011000", 
                    /**//* F */ "001011000",
                    /**//* G */ "000001101", 
                    /**//* H */ "100001100", 
                    /**//* I */ "001001100", 
                    /**//* J */ "000011100",
                    /**//* K */ "100000011", 
                    /**//* L */ "001000011", 
                    /**//* M */ "101000010", 
                    /**//* N */ "000010011",
                    /**//* O */ "100010010", 
                    /**//* P */ "001010010", 
                    /**//* Q */ "000000111", 
                    /**//* R */ "100000110",
                    /**//* S */ "001000110", 
                    /**//* T */ "000010110", 
                    /**//* U */ "110000001", 
                    /**//* V */ "011000001",
                    /**//* W */ "111000000", 
                    /**//* X */ "010010001", 
                    /**//* Y */ "110010000", 
                    /**//* Z */ "011010000",
                    /**//* - */ "010000101", 
                    /**//* . */ "110000100", 
                    /**//*' '*/ "011000100",
                    /**//* $ */ "010101000",
                    /**//* / */ "010100010", 
                    /**//* + */ "010001010", 
                    /**//* % */ "000101010", 
                    /**//* * */ "010010100" 
                };
    strSource = strSource.ToUpper();
    //實作圖片
    Bitmap objBitmap = new Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2), BarCodeHeight + (y * 2));
    //Graphics objGraphics = Graphics.FromImage(objBitmap); //宣告GDI+繪圖介面  
    Graphics objGraphics = Graphics.FromHwnd(this.Handle);
    //填上底色
    objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
    for (int i = 0; i < intSourceLength; i++)
    {
    //檢查是否有非法字元
    if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*')
    {
    objGraphics.DrawString("含有非法字元", SystemFonts.DefaultFont, Brushes.Red, x, y);
    return objBitmap;
    }
    //查表編碼
    strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(strSource[i])]);
    }
    strEncode = string.Format("{0}0010010100", strEncode); //補上結束符號 *  
    int intEncodeLength = strEncode.Length; //編碼後長度
    int intBarWidth;
    objGraphics.DrawString("我的测试字符串", this.Font, SystemBrushes.ControlText, 0, 0);
    for (int i = 0; i < intEncodeLength; i++) //依碼畫出Code39 BarCode
    {
    intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
    objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, y, intBarWidth, BarCodeHeight);
    if (i % 10 == 0)
    {
    int index = i / 10;
    if (index > 0 && index < strSource.Length+1)
    {
    objGraphics.DrawString(strSource[index-1].ToString(), this.Font, SystemBrushes.ControlText, x, y * 2 + 5);
    }
    else
    {
    objGraphics.DrawString("*", this.Font, SystemBrushes.ControlText, x, BarCodeHeight + y);
    }
    }
    x += intBarWidth;
    }
    return objBitmap;
    }
      

  2.   

    谢谢先。不过您修改后的条码显示在窗体的最左上角。
    我修改了objGraphics.FillRectangle(Brushes.White, 50, 50, objBitmap.Width, objBitmap.Height); 底色可以移到理想的位置了,但是画布要怎么移动呢?
    是不是改这一句?Bitmap objBitmap = new Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2), BarCodeHeight + (y * 2));
      

  3.   

    其实这都是坐标的计算了。我使用的是窗体上画的,你要是打印只接在Image里就可以了。
      

  4.   

    最终如下:        private Bitmap GetCode39(string strSource)
            {
                int x = 5; //左边界
                int y = 20; //上边界
                int WidLength = 2; //粗BarCode长度
                int NarrowLength = 1; //细BarCode长度
                int BarCodeHeight = 24; //BarCode高度
                int intSourceLength = strSource.Length;
                string strEncode = "010010100"; //编码字串 初值为 起始符号 *  
                string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母  
                string[] Code39 = //Code39的各字母对应码
                {
                    /**//* 0 */ "000110100", 
                    /**//* 1 */ "100100001", 
                    /**//* 2 */ "001100001", 
                    /**//* 3 */ "101100000",
                    /**//* 4 */ "000110001", 
                    /**//* 5 */ "100110000", 
                    /**//* 6 */ "001110000", 
                    /**//* 7 */ "000100101",
                    /**//* 8 */ "100100100", 
                    /**//* 9 */ "001100100", 
                    /**//* A */ "100001001", 
                    /**//* B */ "001001001",
                    /**//* C */ "101001000", 
                    /**//* D */ "000011001", 
                    /**//* E */ "100011000", 
                    /**//* F */ "001011000",
                    /**//* G */ "000001101", 
                    /**//* H */ "100001100", 
                    /**//* I */ "001001100", 
                    /**//* J */ "000011100",
                    /**//* K */ "100000011", 
                    /**//* L */ "001000011", 
                    /**//* M */ "101000010", 
                    /**//* N */ "000010011",
                    /**//* O */ "100010010", 
                    /**//* P */ "001010010", 
                    /**//* Q */ "000000111", 
                    /**//* R */ "100000110",
                    /**//* S */ "001000110", 
                    /**//* T */ "000010110", 
                    /**//* U */ "110000001", 
                    /**//* V */ "011000001",
                    /**//* W */ "111000000", 
                    /**//* X */ "010010001", 
                    /**//* Y */ "110010000", 
                    /**//* Z */ "011010000",
                    /**//* - */ "010000101", 
                    /**//* . */ "110000100", 
                    /**//*' '*/ "011000100",
                    /**//* $ */ "010101000",
                    /**//* / */ "010100010", 
                    /**//* + */ "010001010", 
                    /**//* % */ "000101010", 
                    /**//* * */ "010010100" 
                };
                strSource = strSource.ToUpper();
                //实作图片
                Bitmap objBitmap = new Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2), BarCodeHeight + (y * 2));//里面的两个参数定义了画布的长和宽
                Graphics objGraphics = Graphics.FromImage(objBitmap); //宣告GDI+绘图界面  
                //Graphics objGraphics = Graphics.FromHwnd(this.Handle);
                //填上底色
                objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
                for (int i = 0; i < intSourceLength; i++)
                {
                    //检查是否有非法字元
                    if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*')
                    {
                        objGraphics.DrawString("含有非法字元", SystemFonts.DefaultFont, Brushes.Red, x, y);
                        return objBitmap;
                    }
                    //查表编码
                    strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(strSource[i])]);
                }
                strEncode = string.Format("{0}0010010100", strEncode); //补上结束符号 *  
                int intEncodeLength = strEncode.Length; //编码后长度
                int intBarWidth;
                objGraphics.DrawString("QZTV-TAPECODE", this.Font, SystemBrushes.ControlText, 0, 0);
                for (int i = 0; i < intEncodeLength; i++) //依码画出Code39 BarCode
                {
                    intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
                    objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, y, intBarWidth, BarCodeHeight);
                    if (i % 10 == 0)
                    {
                        int index = i / 10;
                        if (index > 0 && index < strSource.Length + 1)
                        {
                            objGraphics.DrawString(strSource[index - 1].ToString(), this.Font, SystemBrushes.ControlText, x, y * 2 + 5);
                        }
                        else
                        {
                            objGraphics.DrawString("*", this.Font, SystemBrushes.ControlText, x, BarCodeHeight + y);
                        }
                    }
                    x += intBarWidth;
                }
                return objBitmap;
            }