convert a saved image to byte in an itextsharp image to display in PDF C #

3

I need to convert a saved image into a byte in the database (SQL) to be able to print it to a pdf with itextsharp

In this code I call the method where the byte of the database is consulted.

string imgByte = obtenerByteImagen (codigo, codigoCuentas, imageByte, cone);
            Byte[] img = Convert.FromBase64String(imgByte);

This is the method:

public string obtenerByteImagen(string codigo, string codigoCuentas, string imageByte, SqlConnection cone) {

    SqlDataReader reader;

    try
    {
        cone.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cone;

        cmd.CommandText = "SELECT codigoCuentas FROM dbo.pruebasMil where identificador = '" + codigo + "'";
        reader = cmd.ExecuteReader();
        //reader.Read();

        //cmd.CommandType = CommandType.Text;



        if (reader.Read())
        {
            //Console.WriteLine(String.Format("{0}", reader["nombre"]));

            codigoCuentas = reader["codigoCuentas"].ToString();

            cone.Close();
        }
    }
    catch
    {

    }
    try
    {
        cone.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cone;

        cmd.CommandText = "SELECT logo FROM dbo.cuentas where codigo = '" + codigoCuentas + "'";
        reader = cmd.ExecuteReader();
        //reader.Read();

        //cmd.CommandType = CommandType.Text;



        if (reader.Read())
        {
            //Console.WriteLine(String.Format("{0}", reader["nombre"]));

            imageByte = reader["logo"].ToString();

            cone.Close();
        }
    }
    catch
    {

    }

    return imageByte;
}

Here I turn it into itextsharp image and put it in the document:

iTextSharp.text.Image image12 = iTextSharp.text.Image.GetInstance(img);
document.Add(image12);

The error you give me is the following:

  

System.FormatException: 'The entry is not a valid Base 64 string because it contains a non-Base 64 character, more than two fill characters, or an invalid character between the fill characters. '

In this line of code:

Byte[] img = Convert.FromBase64String(imgByte);
    
asked by Arturo 31.01.2018 в 18:54
source

1 answer

1
  • It is not advisable to take the images or files to the database because It involves a lot of storage space to use, which can bring you problems because your database would grow too much (it's my opnion)
  • To be able to use an image in iTextSharp you could use this piece of code. As you can see we convert the byte [] to an image first and then use it in the report.

    public Image Base64ToImage(string base64String)
    {
        // Convierte tu string 64 a un byte[]
        var imagenBytes = Convert.FromBase64String(base64String);
        var ms = new MemoryStream(imagenBytes, 0,
            imagenBytes.Length);
    
        // Convierte tu byte[] a imagen 
        ms.Write(imagenBytes, 0, imagenBytes.Length);
        var image = Image.FromStream(ms, true);
        return image;
    }
    

    Usage:

    iTextSharp.text.Image image12 = iTextSharp.text.Image.GetInstance(
                       Base64ToImage(
                           obtenerByteImagen (codigo, codigoCuentas, imageByte, cone)
                       ),System.Drawing.Imaging.ImageFormat.Jpeg);
    
  • greetings I hope it helps you

        
    answered by 31.01.2018 / 19:27
    source