This is a simple library that lets you do one thing very easily: generate an Image
for a Code128 BARCODE with a single line of code.
Code128, on the other hand, has out-of-the-box support for all 128 low-order ASCII characters.
Basic usage
Create MVC web application. Below code add to the View which it will take input string and one button to generate BarCode Image.
Now Add the below code in controller (HTTPPOST). Here get the entered text and send that text to
Code128Rendering.MakeBarcodeImage(Bacodetext.Trim(), int.Parse(txtWeight), true)
Method which it will take the input as
There are three parameters:
string InputData:
The message to be encoded
int BarWeight:
The baseline width of the bars in the output. Usually, 1 or 2 is good.
bool AddQuietZone:
If false
, omits the required white space at the start and end of the barcode. If your layout doesn't already provide good margins around the Image
, you should use true
You can save the images in folder location different type (.bmp/ .png / .jpeg)
- [HttpPost]
- public ActionResult Index(string btnSubmit, FormCollection formcoll)
- {
- try
- {
- if (!ReferenceEquals(formcoll["btnGenarate"], null))
- {
- string Bacodetext = formcoll["txtInput"].ToString();
- string txtWeight = "2";
- const int MaxLength = 10;
-
- Image myimg = Code128Rendering.MakeBarcodeImage(Bacodetext.Trim(), int.Parse(txtWeight), true);
- var imgName = Bacodetext;
- if (imgName.Length > MaxLength)
- {
- imgName = imgName.Substring(0, MaxLength);
- }
-
- string ImageFolder = Server.MapPath("~/Images");
- myimg.Save(ImageFolder + "/" + imgName.Trim() + ".bmp");
-
-
- var virtualPath = string.Format("~/Images/{0}.bmp", imgName.Trim());
- ViewBag.Path = virtualPath;
- }
- }
- catch (Exception ex)
- {
- }
- return View();
- }
-
- }
Note: Add library files Code128Content.cs & Code128Rendering.cs in Utility folder. (
That's the GenCode128
namespace, in a static class called Code128Rendering)
Output
Hopefully, this helps you to generate barcode using web application.