liveBook · Manning

Barcode

As explained in .NET in Action, Second Edition

A barcode is a machine-readable visual encoding of data using structured marks or symbols.

A barcode encodes text as structured visual marks that can be generated and returned from a web endpoint as an image. section 3.1.2

A production-oriented barcode endpoint must:

  • Install BarcodeLib 2.4.0 and generate the barcode with a selected symbology such as CODE128. section 3.1.2
  • Choose image format and foreground and background colors before returning encoded image bytes. section 3.1.2
  • Validate input because barcode types impose different restrictions, including numeric-only requirements. section 3.1.2
using BarcodeLib;                                    #1
using System.Drawing;                                #2
using System.Drawing.Imaging;                        #3
  
app.MapGet("/barcode/{text}", (string text) => {     #4
  var barcode = new Barcode();                       #5
  barcode.ImageFormat = ImageFormat.Jpeg;            #6
  _ = barcode.Encode(                                #7
    TYPE.CODE128,                                    #8
    text,                                            #9
    Color.Black,                                     #10
    Color.White);                                    #11
  return Results.File(                               #12
    barcode.Encoded_Image_Bytes,                     #13
    "image/jpeg");                                   #14
});

The focused endpoint listing shows BarcodeLib construction, CODE128 encoding, JPEG configuration, color selection, and Results.File output. section 3.1.2

How .NET in Action, Second Edition covers it

How deep it goes
Implements package configuration and image generation, but leaves type-specific validation as a follow-up task.
Examples
Generates a CODE128 JPEG from route text using black bars on a white background.
What it assumes
Assumes familiarity with ASP.NET Core route mapping, NuGet package references, and HTTP file responses.
What you can do after
Build it from scratch with BarcodeLib 2.4.0 and expose generated images through an HTTP endpoint.
How it connects
Connects barcode generation to ASP.NET Core routes, File ActionResult, image formats, and input validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.