C#控制台下生成EAN13图片N张[含class]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

namespace EAN13
{
    class Program
    {
        static void Main(string[] args)
        {

            int h = 15; int w = 4;
            Bitmap img = new Bitmap(w * 130, h * 60);
            //pictureBox1.Image = (Image)img;

            Graphics g = Graphics.FromImage(img);
            g.FillRectangle(Brushes.White, 0, 0, w * 130, h * 60);
            Pen p = new Pen(Color.White, 0);

            Random rd = new Random();
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    long v1 = rd.Next(100000, 999999);  //只限于:十二位十进制数;
                    long v2 = rd.Next(100000, 999999);  //只限于:十二位十进制数;
                    string s = v1.ToString() + v2.ToString();
                    s = string.Format("{0:D12}", s); //不足十二位的将以零补足;
                    BarCodeEAN13.wctrl_BarCode bc = new BarCodeEAN13.wctrl_BarCode();
                    Rectangle rect = new Rectangle(5+x * 130, y * 60, 120, 50);
                    g.DrawRectangle(p, rect);
                    g.FillRectangle(Brushes.White, rect);
                    bc.Paint_EAN13(s, g, rect);
                }
            }
            //保存为图片文件
            //pictureBox1.Image.Save("g:\\BarCodeEAN13_.bmp");
            img.Save("EAN13_.png");
        }
    }
}

namespace BarCodeEAN13
{
    /// <summary>
    /// Barcode control.
    /// </summary>
    public class wctrl_BarCode
    {
        public string getENA13Code(string Numbers12bit)
        {
            int c1 = 0;
            int c2 = 0;

            for (int i = 0; i < 11; i = i + 2)
            {
                c1 += int.Parse(Numbers12bit[i].ToString());
                c2 += int.Parse(Numbers12bit[i + 1].ToString());
            }

            int c3 = c1 + c2 * 3;

            c3 = c3 - c3 / 10 * 10;

            if (c3 == 0)
            {
                return Numbers12bit + 0;
            }
            else
            {
                int N = 10 - c3;

                return Numbers12bit + N;
            }
        }

        /// <summary>
        /// Paint EAN13 barcode to specified graphics into specified draw rectangle.
        /// </summary>
        /// <param name="barCode">BarCode value.</param>
        /// <param name="g">Graphics where to draw.</param>
        /// <param name="drawBounds">Draw bounds.</param>
        public void Paint_EAN13(string Numbers12bit, Graphics g, Rectangle drawBounds)
        {
            string barCode = getENA13Code(Numbers12bit);

            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 = 1;
            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);
        }

    }
}

转载于:https://my.oschina.net/daode1212/blog/52839

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值