// ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
        // ReSharper restore InconsistentNaming
        {
            var font       = new BCGFont(new Font("Arial", 11, FontStyle.Regular));
            var colorBlack = new BCGColor(0, 0, 0);
            var colorWhite = new BCGColor(255, 255, 255);

            BCGBarcode1D code = new BCGcode39();

            code.setScale(2);                    // Resolution
            code.setThickness(30);               // Thickness
            code.setForegroundColor(colorBlack); // Color of bars
            code.setBackgroundColor(colorWhite); // Color of spaces
            code.setFont(font);                  // Font
            code.parse("HELLO");                 // Text

            /* Here is the list of the arguments
             * 1 - Filename (empty : display on screen)
             * 2 - Background color */
            var drawing = new BCGDrawing(colorWhite);

            drawing.setBarcode(code);
            drawing.draw();

            // Draw (or save) the image into PNG format.
            drawing.finish(ImageFormat.Png, this.Context.Response.OutputStream);
        }
        public static async Task CreateAsync(string filePath, string?text = null)
        {
            // Loading Font
            var font = new BCGFont("Arial", 18);

            // Don't forget to sanitize user inputs
            text = text?.Length > 0 ? text : "PDF417";

            // Label, this part is optional
            var label = new BCGLabel();

            label.SetFont(font);
            label.SetPosition(BCGLabel.Position.Bottom);
            label.SetAlignment(BCGLabel.Alignment.Center);
            label.SetText(text);

            // The arguments are R, G, B for color.
            var colorBlack = new BCGColor(0, 0, 0);
            var colorWhite = new BCGColor(255, 255, 255);

            Exception? drawException = null;
            BCGBarcode?barcode       = null;

            try
            {
                var code = new BCGpdf417();
                code.SetScale(2);
                code.SetRatio(1);
                code.SetErrorLevel(2);
                code.SetCompact(false);
                code.SetQuietZone(true);
                code.SetForegroundColor(colorBlack); // Color of bars
                code.SetBackgroundColor(colorWhite); // Color of spaces

                code.AddLabel(label);

                code.Parse(text);
                barcode = code;
            }
            catch (Exception exception)
            {
                drawException = exception;
            }

            var drawing = new BCGDrawing(barcode, colorWhite);

            if (drawException != null)
            {
                drawing.DrawException(drawException);
            }

            // Saves the barcode into a file.
            await drawing.FinishAsync(BCGDrawing.ImageFormat.Png, filePath);

            // Saves the barcode into a MemoryStream
            ////var memoryStream = new System.IO.MemoryStream();
            ////await drawing.FinishAsync(BCGDrawing.ImageFormat.Png, memoryStream);
        }
        //
        // GET: /Barcode/
        public FileStreamResult Index(string recpId)
        {
            try
            {
                const string fontString = "Arial";
                const int    thickness  = 30;
                const string code       = "Code 128";
                const int    scale      = 1;

                var  font             = new BCGFont(new Font(fontString, 1));
                var  colorBlack       = new BCGColor(Color.Black);
                var  colorWhite       = new BCGColor(Color.White);
                Type codeType         = (from kvp in Utilities.CodeType where kvp.Value == code select kvp.Key).FirstOrDefault();
                var  temporaryBarcode = (BarCode.Barcode)Activator.CreateInstance(codeType);
                var  codebar          = (BCGBarcode1D)Activator.CreateInstance(temporaryBarcode.Code);
                codebar.setThickness(thickness);
                codebar.setScale(scale);
                MethodInfo method = temporaryBarcode.Code.GetMethod("setStart");
                if (method != null)
                {
                    method.Invoke(codebar, new object[] { "A" });
                }
                codebar.setBackgroundColor(colorWhite);
                codebar.setForegroundColor(colorBlack);
                codebar.setFont(font);
                codebar.parse(Utilities.Encrypt(recpId));
                var drawing = new BCGDrawing(colorWhite);
                drawing.setBarcode(codebar);
                drawing.draw();
                Response.ContentType = "image/jpeg";
                drawing.finish(ImageFormat.Jpeg, Response.OutputStream);
            }
            catch (Exception exception)
            {
                Response.ContentType = "image/png";

                /*
                 *              using (var fs = new FileStream("error.png", FileMode.Open))
                 *              {
                 *                  using (var br = new BinaryReader(fs))
                 *                  {
                 *                      var buffer = new byte[fs.Length];
                 *                      br.Read(buffer, 0, (int)fs.Length);
                 *
                 *                      using (var ms = new MemoryStream(buffer))
                 *                      {
                 *                          ms.WriteTo(Response.OutputStream);
                 *                      }
                 *                  }
                 *              }*/
                // Here, check for the exception.
                System.Diagnostics.Debug.Write(exception);
            }
            return(null);
        }
        //
        // GET: /Barcode/
        public FileStreamResult Index(string recpId)
        {
            try
            {
                const string fontString = "Arial";
                const int thickness = 30;
                const string code = "Code 128";
                const int scale = 1;

                var font = new BCGFont(new Font(fontString, 1));
                var colorBlack = new BCGColor(Color.Black);
                var colorWhite = new BCGColor(Color.White);
                Type codeType = (from kvp in Utilities.CodeType where kvp.Value == code select kvp.Key).FirstOrDefault();
                var temporaryBarcode = (BarCode.Barcode)Activator.CreateInstance(codeType);
                var codebar = (BCGBarcode1D)Activator.CreateInstance(temporaryBarcode.Code);
                codebar.setThickness(thickness);
                codebar.setScale(scale);
                MethodInfo method = temporaryBarcode.Code.GetMethod("setStart");
                if (method != null)
                {
                    method.Invoke(codebar, new object[] { "A" });
                }
                codebar.setBackgroundColor(colorWhite);
                codebar.setForegroundColor(colorBlack);
                codebar.setFont(font);
                codebar.parse(Utilities.Encrypt(recpId));
                var drawing = new BCGDrawing(colorWhite);
                drawing.setBarcode(codebar);
                drawing.draw();
                Response.ContentType = "image/jpeg";
                drawing.finish(ImageFormat.Jpeg, Response.OutputStream);
            }
            catch (Exception exception)
            {
                Response.ContentType = "image/png";
                /*
                                using (var fs = new FileStream("error.png", FileMode.Open))
                                {
                                    using (var br = new BinaryReader(fs))
                                    {
                                        var buffer = new byte[fs.Length];
                                        br.Read(buffer, 0, (int)fs.Length);

                                        using (var ms = new MemoryStream(buffer))
                                        {
                                            ms.WriteTo(Response.OutputStream);
                                        }
                                    }
                                }*/
                // Here, check for the exception.
                System.Diagnostics.Debug.Write(exception);
            }
            return null;
        }
        public static async Task CreateAsync(string filePath, string?text = null)
        {
            // Loading Font
            var font = new BCGFont("Arial", 18);

            // Don't forget to sanitize user inputs
            text = text?.Length > 0 ? text : "01900123456789083103001750";

            // The arguments are R, G, B for color.
            var colorBlack = new BCGColor(0, 0, 0);
            var colorWhite = new BCGColor(255, 255, 255);

            Exception? drawException = null;
            BCGBarcode?barcode       = null;

            try
            {
                var code = new BCGdatabarexpanded();
                code.SetScale(1);                    // Resolution
                code.SetStacked(1);
                code.SetForegroundColor(colorBlack); // Color of bars
                code.SetBackgroundColor(colorWhite); // Color of spaces
                code.SetFont(font);                  // Font
                code.SetLinkageFlag(false);
                code.Parse(text);
                barcode = code;
            }
            catch (Exception exception)
            {
                drawException = exception;
            }

            var drawing = new BCGDrawing(barcode, colorWhite);

            if (drawException != null)
            {
                drawing.DrawException(drawException);
            }

            // Saves the barcode into a file.
            await drawing.FinishAsync(BCGDrawing.ImageFormat.Png, filePath);

            // Saves the barcode into a MemoryStream
            ////var memoryStream = new System.IO.MemoryStream();
            ////await drawing.FinishAsync(BCGDrawing.ImageFormat.Png, memoryStream);
        }
        // ReSharper restore InconsistentNaming
        // ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
        {
            var font = new BCGFont(new Font("Arial", 11, FontStyle.Regular));
            var colorBlack = new BCGColor(0, 0, 0);
            var colorWhite = new BCGColor(255, 255, 255);

            BCGBarcode1D code = new BCGcode39();
            code.setScale(2); // Resolution
            code.setThickness(30); // Thickness
            code.setForegroundColor(colorBlack); // Color of bars
            code.setBackgroundColor(colorWhite); // Color of spaces
            code.setFont(font); // Font
            code.parse("HELLO"); // Text

            /* Here is the list of the arguments
            1 - Filename (empty : display on screen)
            2 - Background color */
            var drawing = new BCGDrawing(colorWhite);
            drawing.setBarcode(code);
            drawing.draw();

            // Draw (or save) the image into PNG format.
            drawing.finish(ImageFormat.Png, this.Context.Response.OutputStream);
        }
 /// <summary>
 /// Saves the font.
 /// </summary>
 /// <param name="font">The font.</param>
 public virtual void SetFont(BCGFont font)
 {
     this.font = font;
 }
        // ReSharper restore InconsistentNaming
        // ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string fontString = Request.QueryString["f1"];
                BCGFont font = null;
                if (!string.IsNullOrEmpty(fontString))
                {
                    float fontSize;
                    float.TryParse(Request.QueryString["f2"], out fontSize);
                    font = new BCGFont(new Font(Request.QueryString["f1"], fontSize));
                }

                var colorBlack = new BCGColor(Color.Black);
                var colorWhite = new BCGColor(Color.White);

                string code = Request.QueryString["code"];
                Type codeType = (from kvp in Utility.CodeType where kvp.Value == code select kvp.Key).FirstOrDefault();

                if (codeType == null)
                {
                    throw new Exception("You need to specify a type of barcode");
                }

                var temporaryBarcode = (code.Barcode)Activator.CreateInstance(codeType);
                var codebar = (BCGBarcode1D)Activator.CreateInstance(temporaryBarcode.Code);

                int thickness;
                int.TryParse(Request.QueryString["t"], out thickness);
                codebar.setThickness(thickness);

                int scale;
                int.TryParse(Request.QueryString["r"], out scale);
                codebar.setScale(scale);

                // Special
                int a1;
                if (int.TryParse(Request.QueryString["a1"], out a1) && a1 == 1)
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setChecksum", new[] { typeof(bool) });
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { true });
                    }
                    method = temporaryBarcode.Code.GetMethod("setChecksum", new[] { typeof(int) });
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { 1 });
                    }
                }
                if (!string.IsNullOrEmpty(Request.QueryString["a2"]))
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setStart");
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { Request.QueryString["a2"] });
                    }
                }
                if (!string.IsNullOrEmpty(Request.QueryString["a3"]))
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setLabel");
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { Request.QueryString["a3"] });
                    }
                }

                codebar.setBackgroundColor(colorWhite);
                codebar.setForegroundColor(colorBlack);
                codebar.setFont(font);
                codebar.parse(Request.QueryString["text"]);

                var drawing = new BCGDrawing(colorWhite);
                drawing.setBarcode(codebar);
                drawing.draw();

                // Draw (or save) the image into the right format.
                string format = Request.QueryString["o"];
                ImageFormat imageFormat = null;
                switch (format)
                {
                    case "1":
                        Response.ContentType = "image/bmp";
                        imageFormat = ImageFormat.Bmp;
                        break;
                    case "2":
                        Response.ContentType = "image/png";
                        imageFormat = ImageFormat.Png;
                        break;
                    case "3":
                        Response.ContentType = "image/jpeg";
                        imageFormat = ImageFormat.Jpeg;
                        break;
                    case "4":
                        Response.ContentType = "image/gif";
                        imageFormat = ImageFormat.Gif;
                        break;
                }

                if (imageFormat != null)
                {
                    drawing.finish(imageFormat, Response.OutputStream);
                }
            }
            catch (Exception exception)
            {
                Response.ContentType = "image/png";

                using (var fs = new FileStream("error.png", FileMode.Open))
                {
                    using (var br = new BinaryReader(fs))
                    {
                        var buffer = new byte[fs.Length];
                        br.Read(buffer, 0, (int)fs.Length);

                        using (var ms = new MemoryStream(buffer))
                        {
                            ms.WriteTo(Response.OutputStream);
                        }
                    }
                }

                // Here, check for the exception.
                System.Diagnostics.Debug.Write(exception);
            }
        }
Beispiel #9
0
        // ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
        // ReSharper restore InconsistentNaming
        {
            try
            {
                string  fontString = Request.QueryString["f1"];
                BCGFont font       = null;
                if (!string.IsNullOrEmpty(fontString))
                {
                    float fontSize;
                    float.TryParse(Request.QueryString["f2"], out fontSize);
                    font = new BCGFont(new Font(Request.QueryString["f1"], fontSize));
                }

                var colorBlack = new BCGColor(Color.Black);
                var colorWhite = new BCGColor(Color.White);

                string code     = Request.QueryString["code"];
                Type   codeType = (from kvp in Utility.CodeType where kvp.Value == code select kvp.Key).FirstOrDefault();

                if (codeType == null)
                {
                    throw new Exception("You need to specify a type of barcode");
                }

                var temporaryBarcode = (code.Barcode)Activator.CreateInstance(codeType);
                var codebar          = (BCGBarcode1D)Activator.CreateInstance(temporaryBarcode.Code);

                int thickness;
                int.TryParse(Request.QueryString["t"], out thickness);
                codebar.setThickness(thickness);

                int scale;
                int.TryParse(Request.QueryString["r"], out scale);
                codebar.setScale(scale);

                // Special
                int a1;
                if (int.TryParse(Request.QueryString["a1"], out a1) && a1 == 1)
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setChecksum", new[] { typeof(bool) });
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { true });
                    }
                    method = temporaryBarcode.Code.GetMethod("setChecksum", new[] { typeof(int) });
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { 1 });
                    }
                }
                if (!string.IsNullOrEmpty(Request.QueryString["a2"]))
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setStart");
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { Request.QueryString["a2"] });
                    }
                }
                if (!string.IsNullOrEmpty(Request.QueryString["a3"]))
                {
                    MethodInfo method = temporaryBarcode.Code.GetMethod("setLabel");
                    if (method != null)
                    {
                        method.Invoke(codebar, new object[] { Request.QueryString["a3"] });
                    }
                }

                codebar.setBackgroundColor(colorWhite);
                codebar.setForegroundColor(colorBlack);
                codebar.setFont(font);
                codebar.parse(Request.QueryString["text"]);

                var drawing = new BCGDrawing(colorWhite);
                drawing.setBarcode(codebar);
                drawing.draw();

                // Draw (or save) the image into the right format.
                string      format      = Request.QueryString["o"];
                ImageFormat imageFormat = null;
                switch (format)
                {
                case "1":
                    Response.ContentType = "image/bmp";
                    imageFormat          = ImageFormat.Bmp;
                    break;

                case "2":
                    Response.ContentType = "image/png";
                    imageFormat          = ImageFormat.Png;
                    break;

                case "3":
                    Response.ContentType = "image/jpeg";
                    imageFormat          = ImageFormat.Jpeg;
                    break;

                case "4":
                    Response.ContentType = "image/gif";
                    imageFormat          = ImageFormat.Gif;
                    break;
                }

                if (imageFormat != null)
                {
                    drawing.finish(imageFormat, Response.OutputStream);
                }
            }
            catch (Exception exception)
            {
                Response.ContentType = "image/png";

                using (var fs = new FileStream("error.png", FileMode.Open))
                {
                    using (var br = new BinaryReader(fs))
                    {
                        var buffer = new byte[fs.Length];
                        br.Read(buffer, 0, (int)fs.Length);

                        using (var ms = new MemoryStream(buffer))
                        {
                            ms.WriteTo(Response.OutputStream);
                        }
                    }
                }

                // Here, check for the exception.
                System.Diagnostics.Debug.Write(exception);
            }
        }