Ejemplo n.º 1
0
        public void GenerateImage(string textData, string path)
        {
            GeneratedBarcode barCode = IronBarCode.BarcodeWriter
                                       .CreateBarcode(textData, BarcodeWriterEncoding.Code128)
                                       .ResizeTo(500, 200)
                                       .AddAnnotationTextBelowBarcode(textData)
                                       .SetMargins(10);

            barCode.SaveAsPng(path);
        }
Ejemplo n.º 2
0
        public static void Example1()
        {
            // Generate a Simple BarCode image and save as PNG
            GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("http://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);

            MyBarCode.AddBarcodeValueTextBelowBarcode();
            MyBarCode.SaveAsPng("MyBarCode.png");

            // This line opens the image in your default image viewer
            System.Diagnostics.Process.Start("MyBarCode.png");
        }
        public async Task <ActionResult> AddProduct(AddProductVM model)
        {
            if (ModelState.IsValid)
            {
                Supplier supplier = db.Suppliers.Find(model.SupplierId);
                // Check for and set sku
                // Sku must be Upper case and replace spaces with dashes
                string sku           = "";
                string CustomSkuName = model.Name + model.PackSize;
                if (string.IsNullOrWhiteSpace(model.SkuCode))
                {
                    sku = CustomSkuName.Replace(" ", "-").ToLower();
                }
                else
                {
                    sku = model.SkuCode.Replace(" ", "-").ToLower();
                }

                // Check if sku is unique
                if (db.Products.Any(x => x.SkuCode.Equals(sku)))
                {
                    ModelState.AddModelError("", "The Sku Code already exists");
                    model = new AddProductVM
                    {
                        Categories = new SelectList(db.Categories.ToList(), "Id", "Name"),
                        Suppliers  = new SelectList(db.Suppliers.ToList(), "Id", "Name")
                    };
                    return(View(model));
                }

                // Check for and set slug
                // Slug must be lower case and replace spaces with dashes
                string slug = "";
                string name = sku;
                if (string.IsNullOrWhiteSpace(model.Slug))
                {
                    slug = name.Replace(" ", "-").ToLower();
                }
                else
                {
                    slug = model.Slug.Replace(" ", "-").ToLower();
                }

                // Check if slug and title is unique
                if (db.Products.Any(x => x.Slug.Equals(slug)))
                {
                    ModelState.AddModelError("", "The slug or title already exists");
                    model = new AddProductVM
                    {
                        Categories = new SelectList(db.Categories.ToList(), "Id", "Name"),
                        Suppliers  = new SelectList(db.Suppliers.ToList(), "Id", "Name")
                    };
                    return(View(model));
                }

                #region Upload Image and Generate Barcode
                // Upload Image
                string imgUrl = "";
                try
                {
                    if (model.Image == null)
                    {
                        imgUrl = "/Files/Images/noproduct.png";
                    }
                    else if (model.Image.ContentLength > 0)
                    {
                        var    uploadPath     = "~/Files/Images";
                        string _FileName      = Path.GetFileName(model.Image.FileName);
                        string UniqueFileName = model.SkuCode +
                                                "-" + _FileName.ToLower();

                        string _path = Path.Combine(Server
                                                    .MapPath(uploadPath), UniqueFileName);
                        model.Image.SaveAs(_path);

                        imgUrl = "/Files/Images/" + UniqueFileName;
                    }
                    else
                    {
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

                /*
                 * Generate Barcode
                 */

                // Check if SkuCode exists already
                if (db.Products.Any(x => x.SkuCode.Equals(sku)))
                {
                    ModelState.AddModelError("", "The barcode already exists");
                    return(View(model));
                }

                // Save Barcode to that path
                string _barcodePath = Path.Combine(Server
                                                   .MapPath("~/Files/Barcodes"), sku + ".png");

                // Generate a Simple BarCode image and save as PNG
                //using IronBarCode;
                GeneratedBarcode MyBarCode = BarcodeWriter
                                             .CreateBarcode(sku,
                                                            BarcodeWriterEncoding.Code128);

                MyBarCode.SetMargins(50);

                MyBarCode.SaveAsPng(_barcodePath);

                string _barcodeUrl = "/Files/Barcodes/" + sku + ".png";
                //string _barcodeUrl = "/";
                #endregion

                // Add Product
                db.Products.Add(new Product
                {
                    Name          = model.Name,
                    SkuCode       = sku,
                    CategoryId    = model.CategoryId,
                    Description   = model.Description,
                    Slug          = slug,
                    ImageUrl      = imgUrl,
                    BarcodeUrl    = _barcodeUrl,
                    PackSize      = model.PackSize,
                    Quantity      = 0,
                    SupplierPrice = model.SupplierPrice,
                    SupplierId    = model.SupplierId,
                    SellingPrice  = model.SellingPrice,
                    IsOnSale      = false,
                    Supplier      = supplier
                });

                await db.SaveChangesAsync();

                // Add notification
                var product = await db.Products.OrderByDescending(m => m.Id).FirstOrDefaultAsync();

                db.Notifications.Add(new Notification
                {
                    CreatedDate = DateTime.Now,
                    Message     = "You have added a new product to the pharmacy: " +
                                  product.Name,
                    isRead = false,
                    Icon   = "fa-check",
                    BackgroundColorIcon = "bg-success"
                });

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            model = new AddProductVM
            {
                Categories = new SelectList(db.Categories.ToList(), "Id", "Name"),
                Suppliers  = new SelectList(db.Suppliers.ToList(), "Id", "Name")
            };
            return(View(model));
        }