Follow us

Header Ads

BarCodeGenerator in Asp.net Core

BarCodeGenerator in Asp.net Core

Package

"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" 

It can create BarCodeFile Folder in wwwroot Folder

Controllers[Folder]

HomeController.cs

using BarCodeGenerator.Models;
using IronBarCode;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace BarCodeGenerator.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        //added
        private IWebHostEnvironment _webHostEnvironment;

        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
            _logger = logger;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Index(BarcodeModel model)
        {
            try
            {
                GeneratedBarcode barcode = IronBarCode.BarcodeWriter.CreateBarcode(model.BarcodeText, BarcodeWriterEncoding.Code128);
                //Size of Barcode
                barcode.ResizeTo(500, 150);
                //It can Text of Barcode below barcode Image
                barcode.AddBarcodeValueTextBelowBarcode();
                //It is use for color of Barcode
                barcode.ChangeBarCodeColor(Color.Blue);
                //Set Margins of Barcode
                barcode.SetMargins(10);
                //BarCodeFile folder is created in wwwroot folder
                string path = Path.Combine(_webHostEnvironment.WebRootPath,"BarCodeFile");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //BarCode can save in this address
                string filePath = Path.Combine(_webHostEnvironment.WebRootPath, "BarCodeFile/barcode.png");
                barcode.SaveAsPng(filePath);
                string filename = Path.GetFileName(filePath);
                string imageUrl = $"{this.Request.Scheme}://" + $"{this.Request.Host}{this.Request.PathBase}" + "/BarCodeFile/" + filename;
                ViewBag.barcode = imageUrl;
            }
            catch (Exception)
            {

                throw;
            }
            return View();
        }
        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Models[Folder]

BarcodeModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace BarCodeGenerator.Models
{
    public class BarcodeModel
    {
        [Display(Name="Enter bar code Text")]
        public string BarcodeText { get; set; }
    }
}

Views[Folder]

Home[Folder]

Index.cshtml

@model BarCodeGenerator.Models.BarcodeModel

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<h4>BarcodeModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="BarcodeText" class="control-label"></label>
                <input asp-for="BarcodeText" class="form-control" />
                <span asp-validation-for="BarcodeText" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
            <div class="form-group">
                <img src="@ViewBag.barcode" class="img-thumbnail" />     
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}


Post a Comment

0 Comments