商标码(EAN13)

public void EAN1(string code, int h, int w)
    {
        Bitmap b   = new Bitmap(800, 600);
        Graphics g = Graphics.FromImage(b);
        g.Clear(Color.White);
        Pen p = new Pen(Color.White, 3.0f);
        SolidBrush sb = new SolidBrush(Color.Black);
        SolidBrush sb2 = new SolidBrush(Color.White);
        Font f = new Font("Verdana",12);

        string [] Guide = new string[9]{"AAAAAA","AABABB","AABBAB","ABAABB","ABBAAB","ABBBAA","ABABAB","ABABBA","ABBABA"};
        SortedList Dict = new SortedList();
        Dict.Add("A","0001101001100100100110111101010001101100010101111011101101101110001011");
        Dict.Add("B","0100111011001100110110100001001110101110010000101001000100010010010111");

        string[] Rencode = new string[]{"1110010","1100110","1101100","1000010","1011100","1001110","1010000","1000100","1001000","1110100"};

        string cStart = "101";
        string cMid   = "01010";
        string cEnd   = "101";
        string Barcode;
        string lmethod;
        string bColor;
       
        int rsum=0;
        int lsum=0;
        int sh;
        string s;
        if (w < 2)
            w = 2;
        if (h < 20)
            h = 20;
        int cWidth  = w;
        int cHeight = h;
        //if (code.Length != 13)
        //    Response.Write("必须为13位");
        //for (int i = 0; i < 12; i++)
        //{
        //    if ((i % 2==0))
        //    {
        //         rsum = rsum+(Int32.Parse(code.Substring(i, 1)));
        //    }
        //    else
        //    {
        //         lsum = lsum + (Int32.Parse(code.Substring(i, 1)));
        //    }
        //}
        //if (10 - ((lsum * 3 + rsum) % 10) != (Int32.Parse(code.Substring(12, 1))))
         
        //    Response.Write("条码错误!");

        Barcode = cStart;
        lmethod = code.Substring(0, 1);
        if (lmethod == "0")
            lmethod = "1";
        for (int i = 1; i <7; i++)
        {
            string str = Guide[Int32.Parse(lmethod) - 1];
            string strKey = str.Substring(i-1, 1);
            int n = 7*(Int32.Parse(code.Substring(i, 1)));
            string strDict = Dict[strKey].ToString();

            Barcode = Barcode + strDict.Substring(n, 7);

        }
        string strL = Barcode.Length.ToString();
        Barcode = Barcode + cMid;
        string strLen = Barcode.Length.ToString();

        for (int i = 7; i < 13; i++)
        {
            Barcode = Barcode + Rencode[Int32.Parse(code.Substring(i, 1))];
        }
        Barcode = Barcode + cEnd;

       
        for (int i = 0; i < Barcode.Length; i++)
        {
            if (Barcode.Substring(i, 1) == "1")
            {
                
                if (i < 4 || i > 90 || (i > 45 && i < 50))
                {
                    g.FillRectangle(sb, ((i+1) - 1) * 2 + 32,300,2, 85);
                   
                }
                else
                {
                    g.FillRectangle(sb, ((i + 1) - 1) * 2 + 32, 300, 2, 70);
                 
                }
            }
            else
            {
                g.FillRectangle(sb2, ((i + 1) - 1) * 2 + 32, 300, 2, 80);
                
            }
        }
        g.DrawString(code.Substring(0, 1), f, sb, 12, 370);
        for (int i = 1; i < 7; i++)
        {
            g.DrawString(code.Substring(i, 1), f, sb, (i * 7 + 2) * 2 + 22,370);
            g.DrawString(code.Substring(i + 6, 1), f, sb, (i * 7 + 47) * 2 + 22, 370);
        }
        b.Save(Response.OutputStream, ImageFormat.Gif);
    }

posted @ 2008-02-25 21:26  草市江田  阅读(483)  评论(0)    收藏  举报