Home All Groups Group Topic Archive Search About

Invalid Parameter when retreiving image

Author
16 Nov 2005 3:23 PM
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

Author
16 Nov 2005 3:33 PM
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?
Show quote
"Glen" <gwolin***@millermartin.com> wrote in message
news:1132154593.189794.48150@f14g2000cwb.googlegroups.com...
>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
>
Author
16 Nov 2005 3:50 PM
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

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Author
16 Nov 2005 3:51 PM
Cor Ligthert [MVP]
Glen,

Can you use this sample.

http://www.vb-tips.com/default.aspx?ID=5ad37c7b-3732-4558-8dd7-e68d238952a5

AFAIK can you only use a constructed image as target for your stream

I hope this helps,

Cor
Author
16 Nov 2005 4:16 PM
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)


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

AddThis Social Bookmark Button