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

        }

    }
2
  • What is the type of img field in database? Commented Mar 16, 2014 at 7:38
  • @RagingBull The img field is a Image Type Commented Mar 16, 2014 at 10:08

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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;

1 Comment

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.
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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.