Search code examples
c#barcodebarcode-printing

Spire.Barcode printing barcode from string


I can't manage with my problem with barcode. I'am passing the string from my file in Excel for example 51453125312453. And I'am using Spire.Barcode in my model like this

private void GenerateBarcode()
    {
        BarcodeSettings st = new Spire.Barcode.BarcodeSettings();
        st.Code128SetMode = Spire.Barcode.Code128SetMode.Auto;
        //st.Data = Ilosc.ToString("F6") + "*" + Partia;
        st.TextFont = new System.Drawing.Font("Arial", 16);
        st.ShowTopText = false;
        st.ShowTextOnBottom = true;
        st.ImageWidth = 100;
        Spire.Barcode.BarCodeGenerator bg = new Spire.Barcode.BarCodeGenerator(st);
        HeaderBarcode = bg.GenerateImage().ToString();
        //bg.GenerateImage();
    }

and my string

private string headerBarcode;
    public string HeaderBarcode
    {
        get
        {
            return headerBarcode;
        }
        set
        {
            if (headerBarcode != value)
            {
                headerBarcode = value;
                GenerateBarcode();
                NotifyPropertyChanged(nameof(HeaderBarcode));
            }
        }
    }

and in my print preview a have this System.Drawing.Bitmap instead of barcode. Could you help me to make this right?


Solution

  • Your error in this line:

    HeaderBarcode = bg.GenerateImage().ToString();
    

    GenerateImage() - returns Bitmap, so when you try to convert Bitmap to string you get a name of class System.Drawing.Bitmap.

    You need save as image and open it.

    bg.GenerateImage().Save("Barcode.png");
    System.Diagnostics.Process.Start("Barcode.png");