Exemplo n.º 1
0
        public Bitmap codigo128(string _code, bool vertexto = true, int Height = 0)
        {
            var barcode = new BarcodeCodabar();

            barcode.StartStopText = true;

            barcode.Code = "A" + _code + "B";

            var bm = new System.Drawing.Bitmap(barcode.CreateDrawingImage(Color.Black, Color.White), 200, 30);

            var bmT = new Bitmap(bm.Width + 300, bm.Height + 30);
            var g   = Graphics.FromImage(bmT);

            g.FillRectangle(new SolidBrush(Color.White), 0, 0, bm.Width + 300, bm.Height + 30);

            var pintarTexto = new System.Drawing.Font("Arial", 22);
            var brocha      = (new SolidBrush(Color.Black));

            var stringSize = new SizeF();

            stringSize = g.MeasureString(_code, pintarTexto);
            var centrox = (bm.Width + 250 - stringSize.Width) / 2;
            var x       = centrox;
            var y       = bm.Height;

            var drawformat = new StringFormat();

            drawformat.FormatFlags = StringFormatFlags.NoWrap;
            g.DrawImage(bm, 0, 0);

            var ncode = _code.Substring(0, _code.Length - 0);

            g.DrawString(ncode, pintarTexto, brocha, x, y, drawformat);


            // ConvertBitMapToByteArray(bmT);


            //string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\CodigosBarras\"; // <---
            //if (Directory.Exists(appPath) == false)                                              // <---
            //{                                                                                    // <---
            //    Directory.CreateDirectory(appPath);                                              // <---
            //}                                                                                    // <---

            //appPath += "1005.png";
            //bmT.Save(appPath, ImageFormat.Png);
            //return appPath;
            return(bmT);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取条码图片
        /// </summary>
        /// <param name="content">条码内容</param>
        /// <param name="type">编码类型</param>
        /// <param name="height">高度(磅)</param>
        /// <param name="unit">单位宽度(磅)</param>
        /// <param name="fore">前景色/条码颜色</param>
        /// <param name="back">背景色</param>
        /// <returns>条码Image</returns>
        public static Image getBarcode(string content, BarcodeType type, float height, float unit, Color fore, Color back)
        {
            if (content.isNull())
            {
                content = "1234567";
            }
            try
            {
                var test = Convert.ToInt64(content);
                if (test < 0)
                {
                    content = "1234567";
                }
            }
            catch { content = "1234567"; }
            Image img = null;

            if (fore.A == 0)
            {
                fore = Color.Black;
            }
            if (back.A == 0)
            {
                back = Color.White;
            }
            try
            {
                switch (type)
                {
                case BarcodeType.Code39:
                default:
                    Barcode39 code39 = new Barcode39();
                    code39.BarHeight = height;
                    code39.Code      = content;
                    if (unit > 0)
                    {
                        code39.N = unit;
                    }
                    img = code39.CreateDrawingImage(fore, back);
                    break;

                case BarcodeType.Code128:
                    Barcode128 code128 = new Barcode128();
                    code128.BarHeight = height;
                    code128.Code      = content;
                    if (unit > 0)
                    {
                        code128.N = unit;
                    }
                    code128.CodeType = Barcode.CODE128;
                    img = code128.CreateDrawingImage(fore, back);
                    break;

                case BarcodeType.Inter25:
                    BarcodeInter25 codeInter25 = new BarcodeInter25();
                    codeInter25.BarHeight = height;
                    codeInter25.Code      = content;
                    if (unit > 0)
                    {
                        codeInter25.N = unit;
                    }
                    img = codeInter25.CreateDrawingImage(fore, back);
                    break;

                case BarcodeType.Postnet:
                    BarcodePostnet codePostnet = new BarcodePostnet();
                    codePostnet.BarHeight = height;
                    codePostnet.Code      = content;
                    if (unit > 0)
                    {
                        codePostnet.N = unit;
                    }
                    img = codePostnet.CreateDrawingImage(fore, back);
                    break;

                case BarcodeType.CodeBar:
                    BarcodeCodabar codeCodeBar = new BarcodeCodabar();
                    codeCodeBar.BarHeight = height;
                    codeCodeBar.Code      = "A" + content + "A";
                    if (unit > 0)
                    {
                        codeCodeBar.N = unit;
                    }
                    img = codeCodeBar.CreateDrawingImage(fore, back);
                    break;
                }
            }
            catch { }
            return(img);
        }