I've a problem on this byte[] to image Please could yourclear this for me

C

Cz

Value does not fall within the expected range.

I got this exception when i try to convert byte array back to image

here is my code :

pic = pub.getPicture("1111111111111")

Dim newImage As Image
Dim ms2 As MemoryStream = New MemoryStream(pic, 0, pic.Length)

ms2.Write(pic, 0, pic.Length)
p.Image = New Bitmap(ms2)

And function getPicture do just read image from sql server2005

here is that code :

public byte[] getPicture(String isbn)
{
byte[] pic;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
sqlCmd = "SELECT book_image FROM book WHERE book_isbn = '" + isbn + "'";
SqlCommand picCmd = new SqlCommand(sqlCmd,conn);
pic = (byte[])picCmd.ExecuteScalar();
conn.Close();
picCmd.Dispose();
return pic;
}

Thank all of you guys for any helps
 
G

Guest

The New MemoryStream(pic, 0, pic.Length) already allocates the memory stream
with the passed bytes. You do not need to call ms2.Write. You should put a
break point following the pub.getPicture method to verify a byte array is
returned.

Rick D.
Contractor
 
Top