0

Hi I am storing images in SQL server. The datatype is Image.

while retrieving the Images from the database i getting error like

System.ArgumentException: Parameter is not valid.

The error coming in this line img = Image.FromStream(ms).

private void RetriveImage_Click(object sender, EventArgs e)    
{    
    SqlConnection con = new SqlConnection(constr);        
    con.Open();   
    Image img;

    try
    {
        cmd = new SqlCommand("select Pic from emguimg where ID =" + cbxID.SelectedItem, con);

        SqlDataReader dr = cmd.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(dr);

        byte[] bytes = (byte[])dt.Rows[0]["Pic"];

        MemoryStream ms = new MemoryStream(bytes);
        ms.Write(bytes, 0, bytes.Length);

        img = Image.FromStream(ms);


        PicBox.Image = img;
        PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
        PicBox.BorderStyle = BorderStyle.Fixed3D;

        ms.Flush();
        ms.Close();
    }
    catch (Exception ex)
    {
        WriteLogMessage(ex.ToString());
    }
} 
Andy
  • 3,997
  • 2
  • 19
  • 39
  • `(byte[])((byte[])dt.Rows[0]["Pic"])` is that double cast really necessary? Did you check you have valid bytes that represents a known image format? – leppie Jun 29 '15 at 08:45
  • I don't think sql datatype `Image` is compatible with .NET images. Better to use native bytes to store images in db. –  Jun 29 '15 at 08:48
  • Either your stream is null or the bytes in the stream do not represent a valid image. – Paul Weiland Jun 29 '15 at 08:48
  • 1
    After you wrote on the stream you have to **seek back to its beginning** before you can read that data. Lazy to search _exact_ duplicate but many questions with same problem and different objects. For example: http://stackoverflow.com/a/23472581/1207195 or http://stackoverflow.com/a/20689389/1207195 – Adriano Repetti Jun 29 '15 at 08:50
  • I recommend you to rewrite MemoryStream ms = new MemoryStream(bytes); to using(MemoryStream ms = new MemoryStream(bytes)) { //Here you do the things you need the stream for } after reaching the closing bracket the Stream will get disposed itself and you do not need to call flush() and close() yourself. This works with all object that implement the IDisposable interface. – Paul Weiland Jun 29 '15 at 08:57
  • Duplicate http://stackoverflow.com/questions/676072/image-fromstream-method-returns-invalid-argument-exception – Grigory Kornilov Jun 29 '15 at 09:44
  • Just a small note. `MemoryStream.Flush()` does nothing and can be removed. See https://msdn.microsoft.com/de-de/library/system.io.memorystream.flush(v=vs.110).aspx – Andy Jun 29 '15 at 10:56
  • Hi Thanks for all your kindly reply. I try this also but am getting the same Error.. – Sengottuvelu Muthusamy Jun 30 '15 at 09:44

1 Answers1

0

After filling the MemoryStream seek to position 0 before loading the image from this stream.

ms.Write(bytes, 0, bytes.Length);
ms.Position = 0; //insert this line
img = Image.FromStream(ms);
Cadburry
  • 1,844
  • 10
  • 21