Invalid Parameter when retreiving image

G

Glen

I have a datatable in SQL2005 that stores images. I am able to write
the image data to the row (image type) and it looks correct (afaik).
My problem comes when trying to retreive the image and display it as a
property in my custom object.

Here is my code:

Dim fileByte() As Byte
fileByte = CType(dt.Rows(0)("Image"), Byte())
Dim stream As IO.MemoryStream = New IO.MemoryStream(fileByte)

Me._image = System.Drawing.Image.FromStream(stream)

On the last line, when I try to read from the memory stream to create
the image I get an error: "ArgumentException not handled. Parameter is
not valid."

I've set breakpoints to look at the data. The "fileByte" array IS
getting filled with data and the "stream" is instantiated and not
nothing. But, as soon as I try to set "Me._image", I get the error.

"Me._image" is type System.Drawing.Image

Any help would be grealty appreciated.

Sincerely,
Glen Wolinsky
 
W

W.G. Ryan - MVP

Glen - from the looks of things I'm guessing your problem is with the
stream - not that it doesn't exist but that it may not contain a valid Image
in it. Are you relatively comfortable with the routine that's being used to
save it originally?
 
G

Glen Wolinsky

W.G.,

Thanks for the quick response! Below is some code and explanations
about how I'm saving the image.

1. I have a custom Image object that has some data about the image and
the image itself as a read-only property. _image is type
System.Drawing.Image and _fileStream is type IO.Stream. To SET the
image, you provide my object a filestream and I process it as follows:

Public WriteOnly Property ImageFileStream() As IO.Stream
Set(ByVal value As IO.Stream)
Me._filestream = value
Me._image = System.Drawing.Image.FromStream(Me._filestream)
ReDim imageBytes(CInt(Me._filestream.Length))
Me._filestream.Read(imageBytes, 0,
CInt(Me._filestream.Length))
End Set
End Property

2. Later, when adding the custom Image object, I provide the stored proc
with the _fileStream variable. The column type is SqlDbType.Image:

.AddParameter("@Image", SqlDbType.Image, Me.imageBytes.Length,
Me.imageBytes)

NOTE: This "AddParameter" method is part of a class that wraps our sql
functions. Parm1 is the parameter name, Parm2 is the datatype, Parm3, is
the size, and Parm4 is the data.

Thanks,
Glen
 
G

Glen Wolinsky

Cor,

Thank you, that did the trick. Apparently, the property in my Image
custom object was taking in a "Stream" instead of a "FileStream". I
changed that and THEN filled the byte array with a binary reader. I
then saved this byte array in the table.

Now, when I retrieve the byte array the errors are gone. I'm posting
all my code below. Further comments are welcome.

Thanks again,
Glen

-- Property to take in file stream and create image data.

Public WriteOnly Property ImageFileStream() As IO.FileStream
Set(ByVal value As IO.FileStream)
Me._filestream = value

Dim br As New IO.BinaryReader(Me._filestream)
imageBytes = br.ReadBytes(CInt(Me._filestream.Length))
br.Close()

'> Set image now
Dim ms As New IO.MemoryStream(imageBytes)
Me._image = System.Drawing.Image.FromStream(ms)

End Set
End Property

-- Save imageBytes data from above property

.AddParameter("@Image", SqlDbType.Image, Me.imageBytes.Length,
Me.imageBytes)

-- Retreive image from datatable

Dim fileByte() As Byte
fileByte = CType(dt.Rows(0)("Image"), Byte())
Dim stream As IO.MemoryStream = New IO.MemoryStream(fileByte)

Me._image = System.Drawing.Image.FromStream(stream)
 

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

Top