Convert Text to Bar Code C #

0

I want to convert a text string entered in a TextBox to a barcode, but it has an error:

Error: No se puede convertir implicitamente el tipo 'System.Drawing.image' en 'String'

I use the following instruction:

libraries:

using BarcodeLib;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


   protected void ButtonGenereBarra_Click(object sender, ImageClickEventArgs e)

     {
      BarcodeLib.Barcode Codigobar = new BarcodeLib.Barcode();
      Codigobar.IncludeLabel = true;
      panelResult.BackImageUrl = Codigobar.Encode(BarcodeLib.TYPE.CODE128, TextBoxLeerSerial.Text, Color.Black, Color.White, 400, 100);
     } 

Any ideas of how I can do it or how I can be failing?

    
asked by Andrex_11 03.09.2018 в 22:53
source

2 answers

2

Try this way, what I do is assign the image to a stream and then convert that to a base64 image

System.IO.MemoryStream stream = new System.IO.MemoryStream();
BarcodeLib.Barcode Codigobar = new BarcodeLib.Barcode();
Codigobar.IncludeLabel = true;
System.Drawing.Image img = Codigobar.Encode(BarcodeLib.TYPE.CODE128, TextBoxLeerSerial.Text, Color.Black, Color.White, 400, 100);
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
panelResult.BackImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
    
answered by 04.09.2018 / 18:11
source
1

If you are generating an image you should show it in a PictureBox not a Panel , use the correct controls for each case

PictureBox1.Image = Codigobar.Encode(BarcodeLib.TYPE.CODE128, ...

This way the image that generates the code is shown

It's clear from the property BackImageUrl a url is a string that's why it says Url

    
answered by 04.09.2018 в 03:58