alrightgame
alrightgame

Reputation: 391

IronBarCode stream only

I want to create an QrCode using IronBarCode, and then save it as a Stream or Byte[]. However both methods expect the file to be saved prior to creation:

            var absolute = Request.Scheme + "://" + Request.Host + url;
            var qrcode = IronBarCode.QRCodeWriter.CreateQrCode(absolute);
            qrcode.AddAnnotationTextAboveBarcode(device.Name);
            qrcode.AddBarcodeValueTextBelowBarcode(absolute);
            var f = qrcode.ToJpegStream();
            var y = qrcode.ToJpegBinaryData();

ToJpegStream() and ToJpegBinaryData expects the absolute string to be an actual file path. I want to create a QrCode and save it as a Byte[] or Stream, however the error thrown is "The filename, directory name, or volume label syntax is incorrect."

Upvotes: 2

Views: 878

Answers (2)

Chaknith Bin
Chaknith Bin

Reputation: 9

AddBarcodeValueTextBelowBarcode method parameter for string is FontPath. That is why it was trying to find the font file that does not exist.

string absolute = "https://ironsoftware.com/";
string Name = "Product URL:";

//Add Annotation(text) below the generated barcode
var qrcode = QRCodeWriter.CreateQrCode(absolute);
qrcode.AddAnnotationTextBelowBarcode(Name);
qrcode.ToJpegBinaryData();

//Add Barcode value below the generated barcode
var qrcode = QRCodeWriter.CreateQrCode(absolute);
qrcode.AddBarcodeValueTextBelowBarcode();
qrcode.ToJpegBinaryData();

The below image is FontPath. FontPath is actually the directory path that actually lead to the Font file.

enter image description here

//This will add Barcode value into QRCode
.AddBarcodeValueTextBelowBarcode()

If you want to add absolute path to the QRCode you should use

//This will add your text to Barcode
.AddAnnotationTextBelowBarcode(absolute)

For more information on how to use the method please refer to the API reference: https://ironsoftware.com/csharp/barcode/object-reference/api/IronBarCode.GeneratedBarcode.html#IronBarCode_GeneratedBarcode_AddBarcodeValueTextBelowBarcode

Upvotes: 1

It all makes cents
It all makes cents

Reputation: 5009

The issue mentioned isn't reproducible with the code provided. The code below is adapted from your code and the from the example. It's been tested.

Create a new Windows Forms App (.NET Framework)

Download/install NuGet package: Barcode

Add a Button to Form1 (name: btnCreateQRCode)

Add a PictureBox to Form1 (name: pictureBox1)

enter image description here

Add using directives:

  • using IronBarCode;
  • using System.IO;

Form1.cs:

public partial class Form1 : Form
{
    private byte[] _qrCode = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private byte[] CreateBarCode(string url, string annotationText)
    {
        //create new instance
        GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(url);
        qrCode.AddAnnotationTextAboveBarcode(annotationText);
        qrCode.AddBarcodeValueTextBelowBarcode(url);

        byte[] qrCodeBytes = qrCode.ToJpegBinaryData();

        return qrCodeBytes;
    }

    private void btnCreateQRCode_Click(object sender, EventArgs e)
    {
        _qrCode = CreateBarCode("https://www.google.com/search?q=how+to+search", "How To Search");

        using (MemoryStream ms = new MemoryStream(_qrCode))
        {
            pictureBox1.Image = Image.FromStream(ms);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //fit to size
            pictureBox1.Refresh();
        }
    }
}

enter image description here

Resources:

Upvotes: 2

Related Questions