C.R. 使用条形码,

1.将barcode 通过 CODE39DrawImageBord.cs 类生成为 byte[] 变量

2.将byte[] 变量存到sql server中的image 对象中

3.将数据库中的image 变量直接拖到 crystal report 报表上

ok done!

此处采用的是code 39 格式的barcode 编码规则,如果需要支持其他code 如 128编码,只需添加新的cs类,继承iDrawBarcode.cs 就可以了

以下是代码:

引用处的写法:

 //生成image 图片
CODE39DrawImageBord dr = new CODE39DrawImageBord("*" + shipOrder.RefNumber + "*");
entity.BarcodeNumberImage = dr.GetImageBytes();

基类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace ShipFromHereBusiness.Barcode
{
    public abstract class iDrawBarcode
    {
        protected virtual string BordRuleName
        {
            get { return string.Empty; }
        }

        protected virtual System.Collections.Hashtable Roles
        {
            get { return new System.Collections.Hashtable(); }
        }

        string drawString;
        int width = 800;   //画布的宽度(可计算)
        int height = 36;//1CM
        int unitWidth = 1;   //

        int currentLocation = 0;
          bool boolAutoLength = false;

        public iDrawBarcode(string s,int intWidht,int intHeight,int intStepWidth)
        {
            drawString = s;
            width = intWidht;
            height = intHeight;
            unitWidth = intStepWidth;

        }

        public iDrawBarcode(string s)
        {
            drawString = s;
            boolAutoLength = true;
            width = 30+s.Length *18;
           height = 36;
            unitWidth = 1;   
        }


        public virtual byte[] GetImageBytes()
        {
            Bitmap bm = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bm);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            //画布和边的设定  
            g.Clear(Color.White);

            g.DrawRectangle(Pens.White, 0, 0, width, height);
            for (int i = 0; i < drawString.Length; i++)
            {
                this.DrawString(drawString[i].ToString(), g);
            }
            return Bitmap2Bytes(bm);
        }

        public virtual void Draw(System.IO.Stream target)
        {
            Bitmap bm = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bm);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            //画布和边的设定  
            g.Clear(Color.White);

            g.DrawRectangle(Pens.White, 0, 0, width, height);
            for (int i = 0; i < drawString.Length; i++)
            {
                this.DrawString(drawString[i].ToString(), g);
            }
            bm.Save(target, ImageFormat.Jpeg);
        }


        private byte[] Bitmap2Bytes(Bitmap bm)
        {
            MemoryStream strm = new MemoryStream();
            bm.Save(strm, ImageFormat.Jpeg);
            return strm.ToArray();
         
        }
        protected virtual void DrawString(string s, Graphics g)
        {
            System.Collections.Hashtable hash = this.Roles;
            object o = hash[s];
            if (o == null) return;
            char[] chars = o.ToString().ToCharArray();
            if (chars.Length > 9) return;
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush witeBrush = new SolidBrush(Color.White);

            for (int i = 0; i < 5; i++)
            {
                //画第一个   0   黑条
                if (chars[i] == '0')
                {
                    Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
                    g.FillRectangle(blackBrush, re1);
                    currentLocation += unitWidth;
                }
                else
                {
                    Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
                    g.FillRectangle(blackBrush, re1);
                    currentLocation += 3 * unitWidth;
                }
                //画第6个     5   白条
                if ((i + 5) < 9)
                {
                    if (chars[i + 5] == '0')
                    {
                        Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
                        g.FillRectangle(witeBrush, re1);
                        currentLocation += unitWidth;
                    }
                    else
                    {
                        Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
                        g.FillRectangle(witeBrush, re1);
                        currentLocation += 3 * unitWidth;
                    }
                }
            }
            Rectangle re2 = new Rectangle(currentLocation, 0, unitWidth, height);
            g.FillRectangle(witeBrush, re2);
            currentLocation += unitWidth;

        }
    }


}

code 39 编码规则类 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShipFromHereBusiness.Barcode
{
    /**/
    /**/
    /**/
    /// <summary>
    /// CODE39DrawImageBord 的摘要说明
    /// </summary>
    public class CODE39DrawImageBord : iDrawBarcode
    {
        private System.Collections.Hashtable hash = new System.Collections.Hashtable();

        public CODE39DrawImageBord(string s, int intWidht, int intHeight, int intStepWidth) : base(s, intWidht, intHeight, intStepWidth)
        {
        }

        public CODE39DrawImageBord(string s)
            : base(s)
        {
        }

        protected override string BordRuleName
        {
            get { return "CODE39"; }
        }
  

        protected override System.Collections.Hashtable Roles
        {
            get
            {
                if (hash.Count > 0) return hash;
                hash.Add("0", "001100100");
                hash.Add("1", "100010100");
                hash.Add("2", "010010100");
                hash.Add("3", "110000100");
                hash.Add("4", "001010100");
                hash.Add("5", "101000100");
                hash.Add("6", "011000100");
                hash.Add("7", "000110100");

                hash.Add("8", "100100100");
                hash.Add("9", "010100100");
                hash.Add("A", "100010010");
                hash.Add("B", "010010010");
                hash.Add("C", "110000010");
                hash.Add("D", "001010010");
                hash.Add("E", "101000010");

                hash.Add("F", "011000010");
                hash.Add("G", "000110010");
                hash.Add("H", "100100010");
                hash.Add("I", "010100010");
                hash.Add("J", "001100010");
                hash.Add("K", "100010001");
                hash.Add("L", "010010001");

                hash.Add("M", "110000001");
                hash.Add("N", "001010001");
                hash.Add("O", "101000001");
                hash.Add("P", "011000001");
                hash.Add("Q", "000110001");
                hash.Add("R", "100100001");
                hash.Add("S", "010100001");


                hash.Add("T", "001100001");
                hash.Add("U", "100011000");
                hash.Add("V", "010011000");
                hash.Add("W", "110001000");
                hash.Add("X", "001011000");
                hash.Add("Y", "101001000");
                hash.Add("Z", "011001000");


                hash.Add("-", "000111000");
                hash.Add("%", "100101000");
                hash.Add("$", "010101000");
                hash.Add("*", "001101000");

                return hash;
            }
        }
    }


}

分类:

技术点:

相关文章: