Retrieve Image from database problem

hadinatayp

Well-known member
Joined
Feb 8, 2006
Messages
95
Programming Experience
Beginner
I have an image = "WaterLilies.jpg", i tried to save this image to database (MS SQL), and the code which i use to convert and save to database,

VB.NET:
  Dim fs As New FileStream("waterlilies.jpg", FileMode.Open, FileAccess.ReadWrite)
  Dim arrByte(fs.Length) As Byte
  fs.Read(arrByte, 0, fs.Length)

  ' the insert code goes here......
then i retrieve the saved record and try to convert it back to image

VB.NET:
   'retrievement code........
  
   Dim arrByte() as byte = ds.pic(1).images

   Dim ms As New MemoryStream(arrByte, True)
   ms.Write(arrByte, 0, arrByte.Length)

   ' the problem occurs here 
   Dim i As Image = Image.FromStream(ms)

   PictureBox1.Image = i
The error message : The parameter is not valid.
can anybody help me please...
Thank you
 
Seek the memorystream back to start before trying to read it from image.
VB.NET:
ms.seek(0, Begin)
 
it still doesn't work, i changed the retrieval code to
VB.NET:
'retrievement code........
  
   Dim arrByte() as byte = ds.pic(1).images

   Dim ms As New MemoryStream(arrByte, True)
   ms.Write(arrByte, 0, arrByte.Length)

   ms.Seek(0, SeekOrigin.Begin)
   Dim i As Image = Image.FromStream(ms)

   PictureBox1.Image = i
i still got the same error message : Parameter is not valid.
 
In first post you declared arrByte(fs.Length), that's an zero-based array one byte longer than the Length of the filestream, but it doesn't matter in this case with an extra null byte at the end of the stream. You should still debug the bytes. Find how many bytes the image is, find how many bytes are stored in db, find how many bytes are retrieved from db.
 
they show the same size whether saved or retrieved from db, and i still get the parameter is not valid error message.

:(( --stressed!!!!--- :((

oh.... do we have another way of retrieving image from db, besides this one?
 
Back
Top