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

  • Thread starter Thread starter Cz
  • Start date Start date
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top