Show image in Picturebox from SQL [closed]

-2

I saved the image using the MemoryStream and tried to extract it by converting it back to Image using:

byte[] datos = new byte[]; 
Datos = (byte[])row[campo imagen];
MemoryStream ms = new MemoryStream(datos);
Picturebox.Image = Image.Fromstream(ms);

Here is how to insert the image into the database,

    
asked by Christopher R. Santos Cruz 17.01.2016 в 02:11
source

2 answers

1

Seeing the code of the image I would recommend that you use parameters, concatenate in a string the values is not a good practice.

Your code should have the structure

using (SqlConnection conn = new SqlConnection("<connection string>"))  
{  
   conn.Open();  

   string query = "INSERT INTO NombreTabla (campo1, campo2) VALUES (@param1, @param2)";  
   SqlCommand cmd = new SqlCommand(query, conn);  
   cmd.Parameters.AddWithValue("@param1", Convert.ToString(TextBox1.Text));  
   cmd.Parameters.AddWithValue("@param2", Convert.ToInt32(Textbox2.Text));  

   cmd.ExecuteNonQuery();  

}

As you can see, do not concatenate the INSERT or UPDATE values, but assign them to the Parameters list, that's the way your code should look like

I leave an article where I explain how you could do this that I comment

[WinForms] Employees Edition - Burn image in database

    
answered by 17.01.2016 в 15:25
0

I use a utility like this

public class ImageHelper
{
    public static Image ByteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        return Image.FromStream(ms);
    }

    public static byte[] ImageToByteArray(Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, ImageFormat.Jpeg);
        return ms.ToArray();
    }

}

Then if you have the byte array serious

byte[] datos = (byte[])row[campo imagen];
Picturebox1.Image =ImageHelper.ByteArrayToImage(datos);
    
answered by 17.01.2016 в 02:19