Search code examples
c#wpfbarcodecode128

How do I use the NetBarcode library to display an Barcode?


It's the first time I've used library. Hence the question I asked about the library: link

Add a barcode to my label? On the Website it says for a label it goes with it:

var barcode = new Barcode ("543534", Type.Code128, true);

But when I try to assign the value to the label, it doesn't work. So how do I have to use this library to add the barcode to a label?

Because I can't do much here:

Picture

if I want to get a barcode on a label?


Solution

  • The easy way is to define a ValueConverter

    public class StringToBarcodeConverter : IValueConverter
    {
        public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string s)
            {
                var bc = new NetBarcode.Barcode(s, true);
                return bc.GetByteArray();
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    and use that inside the XAML.

    <Image Source="{Binding Barcode, 
      Converter={StaticResource StringToBarcodeConverter}, 
      Mode=OneWay}" />