// 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);
        }
        //
        // 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;
        }
        // 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);
        }
        // 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 #6
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);
            }
        }