Error "The byte array is not a recognized imageformat" reading image of type byte[] from database for itextSharp in C#

Viewed 49

I hope you can help me. I explain the following: I get the photo from the database, which is saved as a byte array:

List<DevolImgModels> list_img = new List<DevolImgModels>();
string sql = "SELECT img.foto FROM contratostimg img";
conexion.conection(constring);

using (MySqlCommand cmd = new MySqlCommand(sql, conexion.con))
{
    cmd.CommandType = CommandType.Text;
    using (MySqlDataReader d = cmd.ExecuteReader())
    {
        while (d.Read())
        {
            list_img.Add(new DevolImgModels
            {
                foto = (byte[])d["foto"] //I put the photo in a list_img list, and photo is of type byte[]
            });
         }
    }
}

conexion.close();

Image foto = Image.GetInstance(fila.foto);
PdfPCell c = new PdfPCell(foto, true);
c.Image.ScaleToFitHeight = false; 
table.AddCell(c);  

//...
//code definitions to create the pdf. I put the dots to save code and go straight to the problem

foreach (var fila in list_img)
{
    //...
       
    Image foto = Image.GetInstance(fila.foto);
    PdfPCell c = new PdfPCell(foto, true);
    c.Image.ScaleToFitHeight = false; 
    table.AddCell(c);     
}

But when passing row.photo(of type byte[]) to Image, it gives an error, that is, in this line of code it gives the error:

Image foto = Image.GetInstance(fila.foto);

And the error it gives is the following:

The byte array is not a recognized imageformat

buggy image: this is the test image

I wish I could fix it, if anyone has any ideas or solutions I would appreciate it.

Best regards.

0 Answers
Related