zoukankan      html  css  js  c++  java
  • winform 打印条码

    一个winform打印功能的示例

    操作步骤:
    1、新建winform项目及创建窗体
    2、拖取 打印 相关控件
    PageSetupDialog 、 PrintDialog 、 PrintDocument 、PrintPreviewDialog
    3、设置上述控件的Document属性为相应的PrintDocument
    4、设置按钮等控件 及 添加相应按钮事件

    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = true;//启用页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
    
        }
    
        //打印设置
        private void btnSetPrint_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog(); 
        }
    
        //打印预览
        private void btnPrePrint_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog(); 
        }
    
        //打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }
    
        //打印内容的设置
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {                        
            ////打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);
    
            ////打印内容 为 局部的 this.groupBox1
            //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
            //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 
    
            //打印内容 为 自定义文本内容 
            Font font = new Font("宋体", 12);
            Brush bru = Brushes.Blue; 
            for (int i = 1; i <= 5; i++)
            {
                e.Graphics.DrawString("Hello world ", font, bru, i*20, i*20);
            }
        }
    }
    winForm打印功能示例

    几种条码打印方法:

    >> itf14条码打印:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    
    namespace Frm.Common
    {
        class ITF14
        {
    
            private string Raw_Data;
    
            private string[] ITF14_Code = { "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" };
    
            public ITF14(string input)
            {
                Raw_Data = input;
    
                CheckDigit();
            }
            /// <summary>
            /// Encode the raw data using the ITF-14 algorithm.
            /// </summary>
            public string Encode_ITF14()
            {
                //check length of input
                string result = "1010";
    
                for (int i = 0; i < Raw_Data.Length; i += 2)
                {
                    bool bars = true;
                    string patternbars = ITF14_Code[Int32.Parse(Raw_Data[i].ToString())];//码图
                    string patternspaces = ITF14_Code[Int32.Parse(Raw_Data[i + 1].ToString())];//空格图
                    string patternmixed = "";// 混合图
    
                    //interleave
                    while (patternbars.Length > 0)
                    {
                        patternmixed += patternbars[0].ToString() + patternspaces[0].ToString();
                        patternbars = patternbars.Substring(1);
                        patternspaces = patternspaces.Substring(1);
                    }//while
    
                    foreach (char c1 in patternmixed)
                    {
                        if (bars)
                        {
                            if (c1 == 'N')
                                result += "1";
                            else
                                result += "11";
                        }//if
                        else
                        {
                            if (c1 == 'N')
                                result += "0";
                            else
                                result += "00";
                        }//else
                        bars = !bars;
                    }//foreach
                }//foreach
    
                //add ending bars
                result += "1101";
                return result;
            }//Encode_ITF14
    
            private void CheckDigit()
            {
                //calculate and include checksum if it is necessary
                if (Raw_Data.Length == 13)
                {
                    int total = 0;
    
                    for (int i = 0; i <= Raw_Data.Length - 1; i++)
                    {
                        int temp = Int32.Parse(Raw_Data.Substring(i, 1));
                        total += temp * ((i == 0 || i % 2 == 0) ? 3 : 1);
                    }//for
    
                    int cs = total % 10;
                    cs = 10 - cs;
                    if (cs == 10)
                        cs = 0;
    
                    Raw_Data += cs.ToString();
                }//if
            }
    
            public Bitmap getFile(string Code)
            {
                if (Code != null)
                {
                    Bitmap saved = Generate_Image(Code);
                    return saved;
                }
                else
                {
                    return null;
                }
            }
    
            //保存文件
            public void saveFile(string Code, string strFile)
            {
                if (Code != null)
                {
                    Bitmap saved = Generate_Image(Code);
                    saved.Save("D:\1.jpg", ImageFormat.Jpeg);
                    saved.Dispose();
                }
            }
    
            private Bitmap Generate_Image(string strBar)
            {
                Bitmap b = null;
    
                int width = 320;
                int height = 40;
                b = new Bitmap(width, height);
    
                //调整条形码的边距
                int bearerwidth = (int)((b.Width) / 12.05);
                int iquietzone = Convert.ToInt32(b.Width * 0.05);
                int iBarWidth = (b.Width - (bearerwidth * 2) - (iquietzone * 2)) / strBar.Length;
                int shiftAdjustment = ((b.Width - (bearerwidth * 2) - (iquietzone * 2)) % strBar.Length) / 2;
    
                if (iBarWidth <= 0 || iquietzone <= 0)
                    throw new Exception("EGENERATE_IMAGE-3: Image size specified not large enough to draw image. (Bar size determined to be less than 1 pixel or quiet zone determined to be less than 1 pixel)");
    
                //draw image
                int pos = 0;
    
                using (Graphics g = Graphics.FromImage(b))
                {
                    //fill background
                    g.Clear(Color.White);
    
                    //lines are fBarWidth wide so draw the appropriate color line vertically
                    using (Pen pen = new Pen(Color.Black, iBarWidth))
                    {
                        pen.Alignment = PenAlignment.Right;
    
                        while (pos < strBar.Length)
                        {
                            //draw the appropriate color line vertically
                            if (strBar[pos] == '1')
                                g.DrawLine(pen, new Point((pos * iBarWidth) + shiftAdjustment + bearerwidth + iquietzone, 0), new Point((pos * iBarWidth) + shiftAdjustment + bearerwidth + iquietzone, height));
    
                            pos++;
                        }//while
    
                    }//using
    
                }//using
    
                return b;
            }//Generate_Image
        }
    }
    itf14打印
    Common.ITF14 itf14 = new Common.ITF14("1234567890123");
    
                 string strBar = itf14.Encode_ITF14();
                Bitmap bmp=  itf14.getFile(strBar);
                if (bmp == null)
                {
                    MessageBox.Show("生成打印条码时发生错误。");
                    return;
                }
                else
                {
                    e.Graphics.DrawImage(bmp, new Point(10, 10));
                  
                }
    itf14打印调用

    说明:输入要的条码可以是13位或者14位(13位的情况系统会自动补齐校验码)

    >> ean13条码打印:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Drawing;
    
    namespace Frm.Common
    {
        public class BarCode_EAN13
        {
            #region 生成条码  EAN13码
    
            public static string getEAN13(string s, int width, int height)
            {
    
                int checkcode_input = -1;//输入的校验码
    
                if (!Regex.IsMatch(s, @"^d{12}$"))
                {
    
                    if (!Regex.IsMatch(s, @"^d{13}$"))
                    {
    
                        return "存在不允许的字符!";
    
                    }
    
                    else
                    {
    
                        checkcode_input = int.Parse(s[12].ToString());
    
                        s = s.Substring(0, 12);
    
                    }
    
                }
    
    
    
                int sum_even = 0;//偶数位之和
    
                int sum_odd = 0;//奇数位之和
    
    
    
                for (int i = 0; i < 12; i++)
                {
    
                    if (i % 2 == 0)
                    {
    
                        sum_odd += int.Parse(s[i].ToString());
    
                    }
    
                    else
                    {
    
                        sum_even += int.Parse(s[i].ToString());
    
                    }
    
                }
    
    
    
                int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码
    
    
    
                if (checkcode_input > 0 && checkcode_input != checkcode)
                {
    
                    return "输入的校验码错误!";
    
                }
    
    
    
                s += checkcode;//变成13位
    
    
    
                // 000000000101左侧42个01010右侧35个校验7个101000000000
    
                // 6        101左侧6位 01010右侧5位 校验1位101000000000
    
    
    
                string result_bin = "";//二进制串
    
                result_bin += "000000000101";
    
    
    
                string type = ean13type(s[0]);
    
                for (int i = 1; i < 7; i++)
                {
    
                    result_bin += ean13(s[i], type[i - 1]);
    
                }
    
                result_bin += "01010";
    
                for (int i = 7; i < 13; i++)
                {
    
                    result_bin += ean13(s[i], 'C');
    
                }
    
                result_bin += "101000000000";
    
    
    
                string result_html = "";//HTML代码
    
                string color = "";//颜色
    
                int height_bottom = width * 5;
    
                foreach (char c in result_bin)
                {
    
                    color = c == '0' ? "#FFFFFF" : "#000000";
    
                    result_html += "<div style="" + width + "px;height:" + height + "px;float:left;background:" + color + ";"></div>";
    
                }
    
                result_html += "<div style="clear:both"></div>";
    
    
    
                result_html += "<div style="float:left;color:#000000;" + (width * 9) + "px;text-align:center;">" + s[0] + "</div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                for (int i = 1; i < 7; i++)
                {
    
                    result_html += "<div style="float:left;" + (width * 7) + "px;color:#000000;text-align:center;">" + s[i] + "</div>";
    
                }
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
    
                for (int i = 7; i < 13; i++)
                {
    
                    result_html += "<div style="float:left;" + (width * 7) + "px;color:#000000;text-align:center;">" + s[i] + "</div>";
    
                }
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
    
                result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
    
                result_html += "<div style="float:left;color:#000000;" + (width * 9) + "px;"></div>";
    
                result_html += "<div style="clear:both"></div>";
    
    
    
                return "<div style="background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';">" + result_html + "</div>";
    
            }
    
    
    
            private static string ean13(char c, char type)
            {
    
                switch (type)
                {
    
                    case 'A':
                        {
    
                            switch (c)
                            {
    
                                case '0': return "0001101";
    
                                case '1': return "0011001";
    
                                case '2': return "0010011";
    
                                case '3': return "0111101";//011101
    
                                case '4': return "0100011";
    
                                case '5': return "0110001";
    
                                case '6': return "0101111";
    
                                case '7': return "0111011";
    
                                case '8': return "0110111";
    
                                case '9': return "0001011";
    
                                default: return "Error!";
    
                            }
    
                        }
    
                    case 'B':
                        {
    
                            switch (c)
                            {
    
                                case '0': return "0100111";
    
                                case '1': return "0110011";
    
                                case '2': return "0011011";
    
                                case '3': return "0100001";
    
                                case '4': return "0011101";
    
                                case '5': return "0111001";
    
                                case '6': return "0000101";//000101
    
                                case '7': return "0010001";
    
                                case '8': return "0001001";
    
                                case '9': return "0010111";
    
                                default: return "Error!";
    
                            }
    
                        }
    
                    case 'C':
                        {
    
                            switch (c)
                            {
    
                                case '0': return "1110010";
    
                                case '1': return "1100110";
    
                                case '2': return "1101100";
    
                                case '3': return "1000010";
    
                                case '4': return "1011100";
    
                                case '5': return "1001110";
    
                                case '6': return "1010000";
    
                                case '7': return "1000100";
    
                                case '8': return "1001000";
    
                                case '9': return "1110100";
    
                                default: return "Error!";
    
                            }
    
                        }
    
                    default: return "Error!";
    
                }
    
            }
    
            private static string ean13type(char c)
            {
    
                switch (c)
                {
    
                    case '0': return "AAAAAA";
    
                    case '1': return "AABABB";
    
                    case '2': return "AABBAB";
    
                    case '3': return "AABBBA";
    
                    case '4': return "ABAABB";
    
                    case '5': return "ABBAAB";
    
                    case '6': return "ABBBAA";//中国
    
                    case '7': return "ABABAB";
    
                    case '8': return "ABABBA";
    
                    case '9': return "ABBABA";
    
                    default: return "Error!";
    
                }
    
            }
            #endregion
    
            public static void Paint_EAN13(string ean13, Graphics g, Rectangle drawBounds)
            {
                string barCode = ean13;
    
                char[] symbols = barCode.ToCharArray();
    
                //--- Validate barCode -------------------------------------------------------------------//
                if (barCode.Length != 13)
                {
                    return;
                }
                foreach (char c in symbols)
                {
                    if (!Char.IsDigit(c))
                    {
                        return;
                    }
                }
    
                //--- Check barcode checksum ------------------------//
                int checkSum = Convert.ToInt32(symbols[12].ToString());
                int calcSum = 0;
                bool one_three = true;
                for (int i = 0; i < 12; i++)
                {
                    if (one_three)
                    {
                        calcSum += (Convert.ToInt32(symbols[i].ToString()) * 1);
                        one_three = false;
                    }
                    else
                    {
                        calcSum += (Convert.ToInt32(symbols[i].ToString()) * 3);
                        one_three = true;
                    }
                }
    
                char[] calcSumChar = calcSum.ToString().ToCharArray();
                if (checkSum != 0 && checkSum != (10 - Convert.ToInt32(calcSumChar[calcSumChar.Length - 1].ToString())))
                {
                    return;
                }
                //--------------------------------------------------//
                //---------------------------------------------------------------------------------------//
    
                Font font = new Font("Microsoft Sans Serif", 8);
    
                // Fill backround with white color
                //   g.Clear(Color.White);
    
                int lineWidth = 2;
                int x = drawBounds.X;
    
                // Paint human readable 1 system symbol code
                g.DrawString(symbols[0].ToString(), font, new SolidBrush(Color.Black), x, drawBounds.Y + drawBounds.Height - 16);
                x += 10;
    
                // Paint left 'guard bars', always same '101'
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
    
                // First number of barcode specifies how to encode each character in the left-hand 
                // side of the barcode should be encoded.
                bool[] leftSideParity = new bool[6];
                switch (symbols[0])
                {
                    case '0':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = true;  // Odd
                        leftSideParity[2] = true;  // Odd
                        leftSideParity[3] = true;  // Odd
                        leftSideParity[4] = true;  // Odd
                        leftSideParity[5] = true;  // Odd
                        break;
                    case '1':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = true;  // Odd
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = true;  // Odd
                        leftSideParity[4] = false; // Even
                        leftSideParity[5] = false; // Even
                        break;
                    case '2':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = true;  // Odd
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = false; // Even
                        leftSideParity[4] = true;  // Odd
                        leftSideParity[5] = false; // Even
                        break;
                    case '3':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = true;  // Odd
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = false; // Even
                        leftSideParity[4] = false; // Even
                        leftSideParity[5] = true;  // Odd
                        break;
                    case '4':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = true;  // Odd
                        leftSideParity[3] = true;  // Odd
                        leftSideParity[4] = false; // Even
                        leftSideParity[5] = false; // Even
                        break;
                    case '5':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = true;  // Odd
                        leftSideParity[4] = true;  // Odd
                        leftSideParity[5] = false; // Even
                        break;
                    case '6':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = false; // Even
                        leftSideParity[4] = true;  // Odd
                        leftSideParity[5] = true;  // Odd
                        break;
                    case '7':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = true;  // Odd
                        leftSideParity[3] = false; // Even
                        leftSideParity[4] = true;  // Odd
                        leftSideParity[5] = false; // Even
                        break;
                    case '8':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = true;  // Odd
                        leftSideParity[3] = false; // Even
                        leftSideParity[4] = false; // Even
                        leftSideParity[5] = true;  // Odd
                        break;
                    case '9':
                        leftSideParity[0] = true;  // Odd
                        leftSideParity[1] = false; // Even
                        leftSideParity[2] = false; // Even
                        leftSideParity[3] = true;  // Odd
                        leftSideParity[4] = false; // Even
                        leftSideParity[5] = true;  // Odd
                        break;
                }
    
                // second number system digit + 5 symbol manufacter code
                string lines = "";
                for (int i = 0; i < 6; i++)
                {
                    bool oddParity = leftSideParity[i];
                    if (oddParity)
                    {
                        switch (symbols[i + 1])
                        {
                            case '0':
                                lines += "0001101";
                                break;
                            case '1':
                                lines += "0011001";
                                break;
                            case '2':
                                lines += "0010011";
                                break;
                            case '3':
                                lines += "0111101";
                                break;
                            case '4':
                                lines += "0100011";
                                break;
                            case '5':
                                lines += "0110001";
                                break;
                            case '6':
                                lines += "0101111";
                                break;
                            case '7':
                                lines += "0111011";
                                break;
                            case '8':
                                lines += "0110111";
                                break;
                            case '9':
                                lines += "0001011";
                                break;
                        }
                    }
                    // Even parity
                    else
                    {
                        switch (symbols[i + 1])
                        {
                            case '0':
                                lines += "0100111";
                                break;
                            case '1':
                                lines += "0110011";
                                break;
                            case '2':
                                lines += "0011011";
                                break;
                            case '3':
                                lines += "0100001";
                                break;
                            case '4':
                                lines += "0011101";
                                break;
                            case '5':
                                lines += "0111001";
                                break;
                            case '6':
                                lines += "0000101";
                                break;
                            case '7':
                                lines += "0010001";
                                break;
                            case '8':
                                lines += "0001001";
                                break;
                            case '9':
                                lines += "0010111";
                                break;
                        }
                    }
                }
    
                // Paint human readable left-side 6 symbol code
              //  g.DrawString(barCode.Substring(1, 6), font, new SolidBrush(Color.Black), x, drawBounds.Y + drawBounds.Height - 12);
    
                char[] xxx = lines.ToCharArray();
                for (int i = 0; i < xxx.Length; i++)
                {
                    if (xxx[i] == '1')
                    {
                        g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height - 12);
                    }
                    else
                    {
                        g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height - 12);
                    }
                    x += lineWidth;
                }
    
                // Paint center 'guard bars', always same '01010'
                g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
    
                // 5 symbol product code + 1 symbol parity
                lines = "";
                for (int i = 7; i < 13; i++)
                {
                    switch (symbols[i])
                    {
                        case '0':
                            lines += "1110010";
                            break;
                        case '1':
                            lines += "1100110";
                            break;
                        case '2':
                            lines += "1101100";
                            break;
                        case '3':
                            lines += "1000010";
                            break;
                        case '4':
                            lines += "1011100";
                            break;
                        case '5':
                            lines += "1001110";
                            break;
                        case '6':
                            lines += "1010000";
                            break;
                        case '7':
                            lines += "1000100";
                            break;
                        case '8':
                            lines += "1001000";
                            break;
                        case '9':
                            lines += "1110100";
                            break;
                    }
                }
    
                // Paint human readable left-side 6 symbol code
              //  g.DrawString(barCode.Substring(7, 6), font, new SolidBrush(Color.Black), x, drawBounds.Y + drawBounds.Height - 12);
    
                xxx = lines.ToCharArray();
                for (int i = 0; i < xxx.Length; i++)
                {
                    if (xxx[i] == '1')
                    {
                        g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height - 12);
                    }
                    else
                    {
                        g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height - 12);
                    }
                    x += lineWidth;
                }
    
                // Paint right 'guard bars', always same '101'
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.White, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
                x += lineWidth;
                g.DrawLine(new Pen(Color.Black, lineWidth), x, drawBounds.Y, x, drawBounds.Y + drawBounds.Height);
            }
    
    
        }
    
        }
    ean-13条码打印

    说明:输入条码必须是13位,且最后一位的校验码正确

    >> 128条码打印:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Drawing;
    
    namespace Frm.Common
    {
        class BarCode
        {
            public class Code128
            {
                private DataTable m_Code128 = new DataTable();
                private uint m_Height = 40;
                /// <summary>
                /// 高度
                /// </summary>
                public uint Height { get { return m_Height; } set { m_Height = value; } }
                private Font m_ValueFont = null;
                /// <summary>
                /// 是否显示可见号码  如果为NULL不显示号码
                /// </summary>
                public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
                private byte m_Magnify = 0;
                /// <summary>
                /// 放大倍数
                /// </summary>
                public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
                /// <summary>
                /// 条码类别
                /// </summary>
                public enum Encode
                {
                    Code128A,
                    Code128B,
                    Code128C,
                    EAN128
                }
                public Code128()
                {
                    m_Code128.Columns.Add("ID");
                    m_Code128.Columns.Add("Code128A");
                    m_Code128.Columns.Add("Code128B");
                    m_Code128.Columns.Add("Code128C");
                    m_Code128.Columns.Add("BandCode");
                    m_Code128.CaseSensitive = true;
                    #region 数据表
                    m_Code128.Rows.Add("0", " ", " ", "00", "212222");
                    m_Code128.Rows.Add("1", "!", "!", "01", "222122");
                    m_Code128.Rows.Add("2", """, """, "02", "222221");
                    m_Code128.Rows.Add("3", "#", "#", "03", "121223");
                    m_Code128.Rows.Add("4", "$", "$", "04", "121322");
                    m_Code128.Rows.Add("5", "%", "%", "05", "131222");
                    m_Code128.Rows.Add("6", "&", "&", "06", "122213");
                    m_Code128.Rows.Add("7", "'", "'", "07", "122312");
                    m_Code128.Rows.Add("8", "(", "(", "08", "132212");
                    m_Code128.Rows.Add("9", ")", ")", "09", "221213");
                    m_Code128.Rows.Add("10", "*", "*", "10", "221312");
                    m_Code128.Rows.Add("11", "+", "+", "11", "231212");
                    m_Code128.Rows.Add("12", ",", ",", "12", "112232");
                    m_Code128.Rows.Add("13", "-", "-", "13", "122132");
                    m_Code128.Rows.Add("14", ".", ".", "14", "122231");
                    m_Code128.Rows.Add("15", "/", "/", "15", "113222");
                    m_Code128.Rows.Add("16", "0", "0", "16", "123122");
                    m_Code128.Rows.Add("17", "1", "1", "17", "123221");
                    m_Code128.Rows.Add("18", "2", "2", "18", "223211");
                    m_Code128.Rows.Add("19", "3", "3", "19", "221132");
                    m_Code128.Rows.Add("20", "4", "4", "20", "221231");
                    m_Code128.Rows.Add("21", "5", "5", "21", "213212");
                    m_Code128.Rows.Add("22", "6", "6", "22", "223112");
                    m_Code128.Rows.Add("23", "7", "7", "23", "312131");
                    m_Code128.Rows.Add("24", "8", "8", "24", "311222");
                    m_Code128.Rows.Add("25", "9", "9", "25", "321122");
                    m_Code128.Rows.Add("26", ":", ":", "26", "321221");
                    m_Code128.Rows.Add("27", ";", ";", "27", "312212");
                    m_Code128.Rows.Add("28", "<", "<", "28", "322112");
                    m_Code128.Rows.Add("29", "=", "=", "29", "322211");
                    m_Code128.Rows.Add("30", ">", ">", "30", "212123");
                    m_Code128.Rows.Add("31", "?", "?", "31", "212321");
                    m_Code128.Rows.Add("32", "@", "@", "32", "232121");
                    m_Code128.Rows.Add("33", "A", "A", "33", "111323");
                    m_Code128.Rows.Add("34", "B", "B", "34", "131123");
                    m_Code128.Rows.Add("35", "C", "C", "35", "131321");
                    m_Code128.Rows.Add("36", "D", "D", "36", "112313");
                    m_Code128.Rows.Add("37", "E", "E", "37", "132113");
                    m_Code128.Rows.Add("38", "F", "F", "38", "132311");
                    m_Code128.Rows.Add("39", "G", "G", "39", "211313");
                    m_Code128.Rows.Add("40", "H", "H", "40", "231113");
                    m_Code128.Rows.Add("41", "I", "I", "41", "231311");
                    m_Code128.Rows.Add("42", "J", "J", "42", "112133");
                    m_Code128.Rows.Add("43", "K", "K", "43", "112331");
                    m_Code128.Rows.Add("44", "L", "L", "44", "132131");
                    m_Code128.Rows.Add("45", "M", "M", "45", "113123");
                    m_Code128.Rows.Add("46", "N", "N", "46", "113321");
                    m_Code128.Rows.Add("47", "O", "O", "47", "133121");
                    m_Code128.Rows.Add("48", "P", "P", "48", "313121");
                    m_Code128.Rows.Add("49", "Q", "Q", "49", "211331");
                    m_Code128.Rows.Add("50", "R", "R", "50", "231131");
                    m_Code128.Rows.Add("51", "S", "S", "51", "213113");
                    m_Code128.Rows.Add("52", "T", "T", "52", "213311");
                    m_Code128.Rows.Add("53", "U", "U", "53", "213131");
                    m_Code128.Rows.Add("54", "V", "V", "54", "311123");
                    m_Code128.Rows.Add("55", "W", "W", "55", "311321");
                    m_Code128.Rows.Add("56", "X", "X", "56", "331121");
                    m_Code128.Rows.Add("57", "Y", "Y", "57", "312113");
                    m_Code128.Rows.Add("58", "Z", "Z", "58", "312311");
                    m_Code128.Rows.Add("59", "[", "[", "59", "332111");
                    m_Code128.Rows.Add("60", "\", "\", "60", "314111");
                    m_Code128.Rows.Add("61", "]", "]", "61", "221411");
                    m_Code128.Rows.Add("62", "^", "^", "62", "431111");
                    m_Code128.Rows.Add("63", "_", "_", "63", "111224");
                    m_Code128.Rows.Add("64", "NUL", "`", "64", "111422");
                    m_Code128.Rows.Add("65", "SOH", "a", "65", "121124");
                    m_Code128.Rows.Add("66", "STX", "b", "66", "121421");
                    m_Code128.Rows.Add("67", "ETX", "c", "67", "141122");
                    m_Code128.Rows.Add("68", "EOT", "d", "68", "141221");
                    m_Code128.Rows.Add("69", "ENQ", "e", "69", "112214");
                    m_Code128.Rows.Add("70", "ACK", "f", "70", "112412");
                    m_Code128.Rows.Add("71", "BEL", "g", "71", "122114");
                    m_Code128.Rows.Add("72", "BS", "h", "72", "122411");
                    m_Code128.Rows.Add("73", "HT", "i", "73", "142112");
                    m_Code128.Rows.Add("74", "LF", "j", "74", "142211");
                    m_Code128.Rows.Add("75", "VT", "k", "75", "241211");
                    m_Code128.Rows.Add("76", "FF", "I", "76", "221114");
                    m_Code128.Rows.Add("77", "CR", "m", "77", "413111");
                    m_Code128.Rows.Add("78", "SO", "n", "78", "241112");
                    m_Code128.Rows.Add("79", "SI", "o", "79", "134111");
                    m_Code128.Rows.Add("80", "DLE", "p", "80", "111242");
                    m_Code128.Rows.Add("81", "DC1", "q", "81", "121142");
                    m_Code128.Rows.Add("82", "DC2", "r", "82", "121241");
                    m_Code128.Rows.Add("83", "DC3", "s", "83", "114212");
                    m_Code128.Rows.Add("84", "DC4", "t", "84", "124112");
                    m_Code128.Rows.Add("85", "NAK", "u", "85", "124211");
                    m_Code128.Rows.Add("86", "SYN", "v", "86", "411212");
                    m_Code128.Rows.Add("87", "ETB", "w", "87", "421112");
                    m_Code128.Rows.Add("88", "CAN", "x", "88", "421211");
                    m_Code128.Rows.Add("89", "EM", "y", "89", "212141");
                    m_Code128.Rows.Add("90", "SUB", "z", "90", "214121");
                    m_Code128.Rows.Add("91", "ESC", "{", "91", "412121");
                    m_Code128.Rows.Add("92", "FS", "|", "92", "111143");
                    m_Code128.Rows.Add("93", "GS", "}", "93", "111341");
                    m_Code128.Rows.Add("94", "RS", "~", "94", "131141");
                    m_Code128.Rows.Add("95", "US", "DEL", "95", "114113");
                    m_Code128.Rows.Add("96", "FNC3", "FNC3", "96", "114311");
                    m_Code128.Rows.Add("97", "FNC2", "FNC2", "97", "411113");
                    m_Code128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311");
                    m_Code128.Rows.Add("99", "CODEC", "CODEC", "99", "113141");
                    m_Code128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131");
                    m_Code128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141");
                    m_Code128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131");
                    m_Code128.Rows.Add("103", "StartA", "StartA", "StartA", "211412");
                    m_Code128.Rows.Add("104", "StartB", "StartB", "StartB", "211214");
                    m_Code128.Rows.Add("105", "StartC", "StartC", "StartC", "211232");
                    m_Code128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112");
                    #endregion
                }
                /// <summary>
                /// 获取128图形
                /// </summary>
                /// <param name="p_Text">文字</param>
                /// <param name="p_Code">编码</param>      
                /// <returns>图形</returns>
                public Bitmap GetCodeImage(string p_Text, Encode p_Code)
                {
                    string _ViewText = p_Text;
                    string _Text = "";
                    IList<int> _TextNumb = new List<int>();
                    int _Examine = 0;  //首位
                    switch (p_Code)
                    {
                        case Encode.Code128C:
                            _Examine = 105;
                            if (!((p_Text.Length & 1) == 0)) throw new Exception("128C长度必须是偶数");
                            while (p_Text.Length != 0)
                            {
                                int _Temp = 0;
                                try
                                {
                                    int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
                                }
                                catch
                                {
                                    throw new Exception("128C必须是数字!");
                                }
                                _Text += GetValue(p_Code, p_Text.Substring(0, 2), ref _Temp);
                                _TextNumb.Add(_Temp);
                                p_Text = p_Text.Remove(0, 2);
                            }
                            break;
                        case Encode.EAN128:
                            _Examine = 105;
                            if (!((p_Text.Length & 1) == 0)) throw new Exception("EAN128长度必须是偶数");
                            _TextNumb.Add(102);
                            _Text += "411131";
                            while (p_Text.Length != 0)
                            {
                                int _Temp = 0;
                                try
                                {
                                    int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
                                }
                                catch
                                {
                                    throw new Exception("128C必须是数字!");
                                }
                                _Text += GetValue(Encode.Code128C, p_Text.Substring(0, 2), ref _Temp);
                                _TextNumb.Add(_Temp);
                                p_Text = p_Text.Remove(0, 2);
                            }
                            break;
                        default:
                            if (p_Code == Encode.Code128A)
                            {
                                _Examine = 103;
                            }
                            else
                            {
                                _Examine = 104;
                            }
    
                            while (p_Text.Length != 0)
                            {
                                int _Temp = 0;
                                string _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1), ref _Temp);
                                if (_ValueCode.Length == 0) throw new Exception("无效的字符集!" + p_Text.Substring(0, 1).ToString());
                                _Text += _ValueCode;
                                _TextNumb.Add(_Temp);
                                p_Text = p_Text.Remove(0, 1);
                            }
                            break;
                    }
                    if (_TextNumb.Count == 0) throw new Exception("错误的编码,无数据");
                    _Text = _Text.Insert(0, GetValue(_Examine)); //获取开始位
    
                    for (int i = 0; i != _TextNumb.Count; i++)
                    {
                        _Examine += _TextNumb[i] * (i + 1);
                    }
                    _Examine = _Examine % 103;           //获得严效位
                    _Text += GetValue(_Examine);  //获取严效位
                    _Text += "2331112"; //结束位
                    Bitmap _CodeImage = GetImage(_Text);
                    GetViewText(_CodeImage, _ViewText);
                    return _CodeImage;
                }
                /// <summary>
                /// 获取目标对应的数据
                /// </summary>
                /// <param name="p_Code">编码</param>
                /// <param name="p_Value">数值 A b  30</param>
                /// <param name="p_SetID">返回编号</param>
                /// <returns>编码</returns>
                private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
                {
                    if (m_Code128 == null) return "";
                    DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
                    if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString());
                    p_SetID = Int32.Parse(_Row[0]["ID"].ToString());
                    return _Row[0]["BandCode"].ToString();
                }
                /// <summary>
                /// 根据编号获得条纹
                /// </summary>
                /// <param name="p_CodeId"></param>
                /// <returns></returns>
                private string GetValue(int p_CodeId)
                {
                    DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
                    if (_Row.Length != 1) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
                    return _Row[0]["BandCode"].ToString();
                }
                /// <summary>
                /// 获得条码图形
                /// </summary>
                /// <param name="p_Text">文字</param>
                /// <returns>图形</returns>
                private Bitmap GetImage(string p_Text)
                {
                    char[] _Value = p_Text.ToCharArray();
                    int _Width = 0;
                    for (int i = 0; i != _Value.Length; i++)
                    {
                        _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);
                    }
                    Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
                    Graphics _Garphics = Graphics.FromImage(_CodeImage);
                    //Pen _Pen;
                    int _LenEx = 0;
                    for (int i = 0; i != _Value.Length; i++)
                    {
                        int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);  //获取宽和放大系数
                        if (!((i & 1) == 0))
                        {
                            //_Pen = new Pen(Brushes.White, _ValueNumb);
                            _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                        }
                        else
                        {
                            //_Pen = new Pen(Brushes.Black, _ValueNumb);
                            _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                        }
                        //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
                        _LenEx += _ValueNumb;
                    }
                    _Garphics.Dispose();
                    return _CodeImage;
                }
                /// <summary>
                /// 显示可见条码文字 如果小于40 不显示文字
                /// </summary>
                /// <param name="p_Bitmap">图形</param>           
                private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
                {
                    if (m_ValueFont == null) return;
    
                    Graphics _Graphics = Graphics.FromImage(p_Bitmap);
                    SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
                    if (_DrawSize.Height > p_Bitmap.Height - 10 || _DrawSize.Width > p_Bitmap.Width)
                    {
                        _Graphics.Dispose();
                        return;
                    }
    
                    int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
                    _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
                    _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY);
                }
    
                //12345678
                //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
                //结果为starc +12 +34 +56 +78 +47 +end
    
                internal Image GetCodeImage(string p)
                {
                    throw new NotImplementedException();
                }
            }
    
        }
    }
    128条码打印

    说明:128条码打印不需要校验位,但与ean-13条码相比,128条码长度过长,有时会降低扫描的准确度

    >> 39条码打印:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Text.RegularExpressions;
    using System.Drawing.Imaging;
    using System.Drawing;
    
    namespace Frm.Common
    {
        public class createcode
        {
            #region 条形码 2014年9月12日11:27:57加
    
            //對應碼表
            public Hashtable Decode;
            public Hashtable CheckCode;
            //每個字元間的間隔符
            private string SPARATOR = "0";
            public int LineHeight = 60;
            public int xCoordinate = 0;//75; //條碼起始座標
            public int WidthCU = 4; //粗線和寬間隙寬度
            public int WidthXI = 1; //細線和窄間隙寬度
            private int Height = 0;
            private int Width = 0;
    
            #region 碼表
            public void Inits()
            {
                Decode = new Hashtable();
                #region 添加值
                Decode.Add("0", "000110100");
                Decode.Add("1", "100100001");
                Decode.Add("2", "001100001");
                Decode.Add("3", "101100000");
                Decode.Add("4", "000110001");
                Decode.Add("5", "100110000");
                Decode.Add("6", "001110000");
                Decode.Add("7", "000100101");
                Decode.Add("8", "100100100");
                Decode.Add("9", "001100100");
                Decode.Add("A", "100001001");
                Decode.Add("B", "001001001");
                Decode.Add("C", "101001000");
                Decode.Add("D", "000011001");
                Decode.Add("E", "100011000");
                Decode.Add("F", "001011000");
                Decode.Add("G", "000001101");
                Decode.Add("H", "100001100");
                Decode.Add("I", "001001101");
                Decode.Add("J", "000011100");
                Decode.Add("K", "100000011");
                Decode.Add("L", "001000011");
                Decode.Add("M", "101000010");
                Decode.Add("N", "000010011");
                Decode.Add("O", "100010010");
                Decode.Add("P", "001010010");
                Decode.Add("Q", "000000111");
                Decode.Add("R", "100000110");
                Decode.Add("S", "001000110");
                Decode.Add("T", "000010110");
                Decode.Add("U", "110000001");
                Decode.Add("V", "011000001");
                Decode.Add("W", "111000000");
                Decode.Add("X", "010010001");
                Decode.Add("Y", "110010000");
                Decode.Add("Z", "011010000");
                Decode.Add("-", "010000101");
                Decode.Add("%", "000101010");
                Decode.Add("$", "010101000");
                Decode.Add("*", "010010100");
                #endregion
                CheckCode = new Hashtable();
                #region 添加值
                CheckCode.Add("0", "0");
                CheckCode.Add("1", "1");
                CheckCode.Add("2", "2");
                CheckCode.Add("3", "3");
                CheckCode.Add("4", "4");
                CheckCode.Add("5", "5");
                CheckCode.Add("6", "6");
                CheckCode.Add("7", "7");
                CheckCode.Add("8", "8");
                CheckCode.Add("9", "9");
                CheckCode.Add("A", "10");
                CheckCode.Add("B", "11");
                CheckCode.Add("C", "12");
                CheckCode.Add("D", "13");
                CheckCode.Add("E", "14");
                CheckCode.Add("F", "15");
                CheckCode.Add("G", "16");
                CheckCode.Add("H", "17");
                CheckCode.Add("I", "18");
                CheckCode.Add("J", "19");
                CheckCode.Add("K", "20");
                CheckCode.Add("L", "21");
                CheckCode.Add("M", "22");
                CheckCode.Add("N", "23");
                CheckCode.Add("O", "24");
                CheckCode.Add("P", "25");
                CheckCode.Add("Q", "26");
                CheckCode.Add("R", "27");
                CheckCode.Add("S", "28");
                CheckCode.Add("T", "29");
                CheckCode.Add("U", "30");
                CheckCode.Add("V", "31");
                CheckCode.Add("W", "32");
                CheckCode.Add("X", "33");
                CheckCode.Add("Y", "34");
                CheckCode.Add("Z", "35");
                CheckCode.Add("-", "36");
                CheckCode.Add(".", "37");
                CheckCode.Add(",", "38");
                CheckCode.Add("$", "39");
                CheckCode.Add("/", "40");
                CheckCode.Add("+", "41");
                CheckCode.Add("%", "42");
                #endregion
            }
            #endregion
    
    
            //保存檔
            public Bitmap CreateBarImage(string Code, int UseCheck, int ValidateCode)
            {
                string code39 = Encode39(Code, UseCheck, ValidateCode);
                if (code39 != null)
                {
                    Bitmap saved = new Bitmap(250, 100);
                    //Bitmap saved = new Bitmap(200, 80);
                    Graphics g = Graphics.FromImage(saved);
                    g.FillRectangle(new SolidBrush(Color.White), 0, 0, this.Width, this.Height);
                    this.DrawBarCode39(code39, g);
                    //string filename = Server.MapPath("\DesktopModules\HospitalCare\Images\" + Code + ".jpg");
                    string filename = "d:\" + Code + ".jpg";
                    return saved;
    
                }
                else
                {
                    return null;
                }
            }
    
            ////保存檔
            //public void saveFile(string Code, int UseCheck, int ValidateCode)
            //{
            //    string code39 = Encode39(Code, UseCheck, ValidateCode);
            //    if (code39 != null)
            //    {
            //        Bitmap saved = new Bitmap(this.Width, this.Height);
            //        //Bitmap saved = new Bitmap(200, 80);
            //        Graphics g = Graphics.FromImage(saved);
            //        g.FillRectangle(new SolidBrush(Color.White), 0, 0, this.Width, this.Height);
            //        this.DrawBarCode39(code39, g);
            //        //string filename = Server.MapPath("\DesktopModules\HospitalCare\Images\" + Code + ".jpg");
            //        string filename = "d:\" + Code + ".jpg";
            //        try
            //        {
            //            saved.Save(filename, ImageFormat.Jpeg);
            //        }
            //        catch
            //        {
            //            //Response.Write("<script>alert('错了!');</script>");
            //            MessageBox.Show("请在本机D盘下建立TXM目录", "错误提示", MessageBoxButtons.OK);
            //        }
            //        saved.Dispose();
            //    }
            //}
    
            private string Encode39(string Code, int UseCheck, int ValidateCode)
            {
                int UseStand = 1; //檢查輸入待編碼字元是否為標準格式(是否以*開始結束)
    
                //保存備份資料
                string originalCode = Code;
    
                //為空不進行編碼
                if (null == Code || Code.Trim().Equals(""))
                {
                    return null;
                }
                //檢查錯誤字元
                Code = Code.ToUpper(); //轉為大寫
                Regex rule = new Regex(@"[^0-9A-Z%$-*]");
                if (rule.IsMatch(Code))
                {
                    MessageBox.Show("編碼中包含非法字元,目前僅支援字母,數位及%$-*符號!!");
    
                    //MessageBox.Show("編碼中包含非法字元,目前僅支援字母,數位及%$-*符號!!");
                    return null;
                }
                //計算檢查碼
                if (UseCheck == 1)
                {
                    int Check = 0;
                    //累計求和
                    for (int i = 0; i < Code.Length; i++)
                    {
                        Check += int.Parse((string)CheckCode[Code.Substring(i, 1)]);
                    }
                    //取模
                    Check = Check % 43;
                    //附加檢測碼
                    if (ValidateCode == 1)
                    {
                        foreach (DictionaryEntry de in CheckCode)
                        {
                            if ((string)de.Value == Check.ToString())
                            {
                                Code = Code + (string)de.Key;
                                break;
                            }
                        }
                    }
                }
                //標準化輸入字元,增加起始標記
                if (UseStand == 1)
                {
                    if (Code.Substring(0, 1) != "*")
                    {
                        Code = "*" + Code;
                    }
                    if (Code.Substring(Code.Length - 1, 1) != "*")
                    {
                        Code = Code + "*";
                    }
                }
                //轉換成39編碼
                string Code39 = string.Empty;
                for (int i = 0; i < Code.Length; i++)
                {
                    Code39 = Code39 + (string)Decode[Code.Substring(i, 1)] + SPARATOR;
                }
    
                int height = 0 + LineHeight;//定義圖片高度      
                int width = xCoordinate;
                for (int i = 0; i < Code39.Length; i++)
                {
                    if ("0".Equals(Code39.Substring(i, 1)))
                    {
                        width += WidthXI;
                    }
                    else
                    {
                        width += WidthCU;
                    }
                }
                this.Width = width + xCoordinate;
                this.Height = height;
    
                return Code39;
            }
            private void DrawBarCode39(string Code39, Graphics g)
            {
                //int UseTitle = 1; //條碼上端顯示標題
                //int UseTTF = 1; //使用TTF字體,方便顯示中文,需要$UseTitle=1時才能生效
                //if (Title.Trim().Equals(""))
                //{
                //    UseTitle = 0;
                //}
                Pen pWhite = new Pen(Color.White, 1);
                Pen pBlack = new Pen(Color.Black, 1);
                int position = xCoordinate;
                //顯示標題
                //if (UseTitle == 1)
                //{
                //    Font TitleFont = new Font("Arial", 8, FontStyle.Regular);
                //    SizeF sf = g.MeasureString(Title, TitleFont);
                //    g.DrawString(Title, TitleFont, Brushes.Black, (Width - sf.Width) / 2, 5);
                //}
                for (int i = 0; i < Code39.Length; i++)
                {
                    //繪製條線
                    if ("0".Equals(Code39.Substring(i, 1)))
                    {
                        for (int j = 0; j < WidthXI; j++)
                        {
                            g.DrawLine(pBlack, position + j, 0, position + j, 0 + LineHeight);
                        }
                        position += WidthXI;
                    }
                    else
                    {
                        for (int j = 0; j < WidthCU; j++)
                        {
                            g.DrawLine(pBlack, position + j, 0, position + j, 0 + LineHeight);
                        }
                        position += WidthCU;
                    }
                    i++;
                    //繪製間隔線
                    if ("0".Equals(Code39.Substring(i, 1)))
                    {
                        position += WidthXI;
                    }
                    else
                    {
                        position += WidthCU;
                    }
                }
                return;
            }
            #endregion
    
        }
    }
    39条码打印
  • 相关阅读:
    代码审计之越权及越权
    代码审计之XSS及修复
    代码审计之SQL注入及修复
    代码审计之CSRF原理及修复
    挖穿各大SRC的短信轰炸
    Kerberoasting攻击
    SPN扫描
    Windows认证 | 域认证
    Windows认证 | 网络认证
    Ceph 纠删码介绍
  • 原文地址:https://www.cnblogs.com/eye-like/p/5144730.html
Copyright © 2011-2022 走看看