4

I'm trying to generate a barcode using ZXing.NET for dot net core asp.net application. I can't figure out how to display text with the barcode and documentation seems to be really, really lacking. Does anyone have an idea how to make it work?

This is the code I have (mostly taken from another post on SO):

BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
{
    Format = BarcodeFormat.CODE_128,
    Options = new EncodingOptions
    {
        Height = 400,
        Width = 800,
        PureBarcode = false, // this should indicate that the text should be displayed, in theory. Makes no difference, though.
        Margin = 10
    }
};

var pixelData = writer.Write("test text");

using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
    using (var ms = new System.IO.MemoryStream())
    {
        var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        try
        {
            System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
        }
        finally
        {
            bitmap.UnlockBits(bitmapData);
        }

        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return File(ms.ToArray(), "image/jpeg");
    }
}

This gets me a barcode, but no content.

Or, suggestions of better/easier to use/better-documented libraries would be appreciated too.

Evgeni
  • 3,341
  • 7
  • 37
  • 64

2 Answers2

4

You don't need to copy those pixels data to another stream manually. Always prefer to using methods provided by the interface , namely the Save() method.

public void YourActionMethod()
{
    BarcodeWriter writer = new BarcodeWriter(){
        Format = BarcodeFormat.CODE_128,
        Options = new EncodingOptions {
            Height = 400,
            Width = 800,
            PureBarcode = false,
            Margin = 10,
        },
    };

    var bitmap = writer.Write("test text");
    bitmap.Save(HttpContext.Response.Body,System.Drawing.Imaging.ImageFormat.Png);
    return; // there's no need to return a `FileContentResult` by `File(...);`
}

Demo :

enter image description here

itminus
  • 23,772
  • 2
  • 53
  • 88
  • I don't use Save because this is a web application and I just need to return the barcode image to the client. – Evgeni Jan 08 '19 at 01:33
  • @Evgeni The `Save()` method here will accept a http response stream as a parameter and then return the barcode image to the client. See the screenshot. – itminus Jan 08 '19 at 01:48
  • I must be missing something. Trying the code above I get a compile error saying that BarcodeWriter is generic and requires a type argument. I've tried using BarcodeWriter, at which point it requires me to specify a renderer. There seems to be no bitmap renderer. I can use PixelData, but PixelData's Save method doesn't have an overload that writes to a stream. Am I in a wrong namespace? – Evgeni Jan 08 '19 at 13:32
  • @Evgeni if you look into the [official sample](https://github.com/micjahn/ZXing.Net/blob/master/Clients/ASP.NetCoreDemo/project.json#L26), you'll find that the author references a compat library for drawing. Simply add a reference to `ZXing.Net.Bindings.CoreCompat.System.Drawing` , and use namespace by `using ZXing; using ZXing.Common; using ZXing.CoreCompat.System.Drawing;` – itminus Jan 08 '19 at 14:49
  • For some reason `bitmap.Save(HttpContext.Response.Body,System.Drawing.Imaging.ImageFormat.Png);` does not return anything to the stream on my side. – Sha May 09 '21 at 11:49
  • Is this need to install ZXing.Net.Mobile? – toha Oct 27 '21 at 07:52
  • @toha No, you don't have to :) – itminus Oct 27 '21 at 09:52
  • On downgrading my Zxing.net nuget package to `0.14.0.1` , this worked. Thanks! – nkanani May 27 '22 at 06:21
1

Not every available renderer implementation supports the output of the content below the barcode (f.e. PixelData renderer doesn't support it). You should use one of the specific implementation for different image libraries. For example the following bindings provider a renderer (and a specific BarcodeWriter) with support for content output: https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility https://www.nuget.org/packages/ZXing.Net.Bindings.ZKWeb.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.SkiaSharp

Michael
  • 2,361
  • 1
  • 15
  • 15