长期做WEB对GDI不熟,画的不对的还请多多指教
    public abstract class DrawImageBord
    {
        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;        public DrawImageBord(string s)
        {
            drawString = s;
        }        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);
        }        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;        }
    }    public class CODE39DrawImageBord : DrawImageBord
    {
        private System.Collections.Hashtable hash = new System.Collections.Hashtable();
        protected override string BordRuleName
        {
            get { return "CODE39"; }
        }
        public CODE39DrawImageBord(string s) : base(s)
        {
            
        }        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;            }
        }
    }//调用
 protected void Page_Load(object sender, EventArgs e)
    {        CODE39DrawImageBord  dr = new CODE39DrawImageBord("*3949178*");
        dr.Draw(Response.OutputStream);
 //然后print用扫描器读.
    }// 39码前后要画*号,如果不画* 号,我这边的扫描器不读.....

解决方案 »

  1.   

    扫描器不读可以设置的....
    :D
    还是建议用CODE39的字体.  为什么要自己画呢?
      

  2.   

    还是建议用CODE39的字体.  为什么要自己画呢?我也查过,但是没敢用,这是要在网页上打印的,是直接在服务器上装了就行么?还是需要客户机和服务器都装?
      

  3.   

    CODE39 和 code128 什么差别
      

  4.   

    为什么不用code39字体呢?这个跟字体安装没有什么关系.你把这个字体COPY到你应用程序的某一目录下.然后通过程序调用指定的字体生成.没有什么不妥的.B/S与C/S的都可以用它来做.