2

Iam doing a winforms application to insert an image to a database(sql server 2008)and retrieve an image from database into a picture box.The code for inserting works perfectly.While the code for retrieving show's up an error parameter not Valid.I was trying various solutions found by goggling but none of them succeeded.

Here is my code for retrieving

           private void button2_Click(object sender, EventArgs e)
            {
 FileStream fs1 = new FileStream("D:\\4usdata.txt", FileMode.OpenOrCreate,FileAccess.Read);
        StreamReader reader = new StreamReader(fs1);
        string id = reader.ReadToEnd();
        reader.Close();
        int ide = int.Parse(id);
        con.Open();
        SqlCommand cmd = new SqlCommand("select img from tempdb where id='" + id + "'", con);
        //cmd.CommandType = CommandType.Text;
        //object ima = cmd.ExecuteScalar();
        //Stream str = new MemoryStream((byte[])ima);
        //pictureBox1.Image = Bitmap.FromStream(str);
        SqlDataAdapter dp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dp.Fill(ds);
        int c = ds.Tables[0].Rows.Count;
        if (c ==1)
        {


            Byte[] MyData = new byte[0];
            MyData = (Byte[])ds.Tables[0].Rows[0]["img"];
            MemoryStream stream = new MemoryStream(MyData);
            stream.Position = 0;
            pictureBox1.Image = Image.FromStream(stream);

        }

    }
Jijith
  • 55
  • 1
  • 2
  • 8

3 Answers3

2

First you must consider that the datatype in your Database of image must be VarBinary:

in your button click event:

byte[] getImg=new byte[0];
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select img from tempdb where id='" + id + "'", con);
cmd.CommandType=CommandType.Text;
DataSet ds = new DataSet();
da.Fill(ds);
foreach(DataRow dr in ds.Tables[0].Rows)
{
   getImg=(byte[])dr["img"];
}

byte[] imgData=getImg;
MemoryStream stream = new MemoryStream(imgData);
pictureBox1.Image=Image.FromStream(stream);
}
JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51
0

Try this code:

To store into database:

public byte[] WinImage=new byte[0];
MemoryStream stream = new MemoryStream();
PictureBox.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
WinImage=stream.ToArray();

And save it to the table as varbinary(max).

To retrieve the image from database:

MemoryStream stream = new MemoryStream(byte[] WinImage);
Image RetImage = Image.FromStream(stream);
PictureBox.Image = RetImage;
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • When do you dispose of `stream`? When I try to dispose it right after setting PictureBox.Image = Image.FromStream(stream) I get an ObjectDisposedException. Leaving the stream around indefinitely doesn't seem wise. – Eric J. Oct 08 '15 at 20:19
0

You can do in this way,

Byte[] MyData = new byte[0];

MyData = (Byte[])ds.Tables[0].Rows[0]["img"];

if (MyData != null && MyData .Length > 0)  
{
    string img = Convert.ToBase64String(MyData , 0, MyData.Length);

    pictureBox1.ImageUrl = "data:image/png;base64," + img;
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
Kailas
  • 439
  • 1
  • 5
  • 14