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);