Search code examples
c#asp.netzxing

Load generated image into asp image control wihtout saving it physically


I'm trying to load an image into an asp image control. That image is generated from zxing Barcode Writter. My question is, can I load it wihtout physically saving it first?

string barcode = "xxxxxx";
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128 };

imgBarCode.ImageUrl = writer.Write(barcode);

... How can I reference writer.Writer to image control "imgBarCode"


Solution

  • With the suggestion made by user1429080 I ended up with this:

    string barcode = "12345"
    
    BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128, Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 300 } };
    
    var bitmap = writer.Write(barcode);    
    MemoryStream ms = new MemoryStream();
    bitmap.Save(ms, ImageFormat.Jpeg);
    var b64 = Convert.ToBase64String(ms.ToArray());
    
    imgBarCode.ImageUrl = "data:image/jpeg;base64," + b64;