System.IO.MemoryStream

Eloff

Freshman
Joined
Mar 20, 2003
Messages
35
Location
Canada
Loading an image from a binary file without FromFile?

I've narrowed the problem down to
picBox.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(image, 0, image.Length))

Where an exception occurs in system.drawing, saying a bad param was given for FromStream(). Maybe I'm going about this the wrong way? How can I load an image from a binary file and convert it from byte() to system.drawing.image? Current code below.


Original Message:

This is what I'm doing, but it tells me I have an invalid parameter and points me to the last line with the memory stream object in it. It says the function accepts byte() and I'm giving it byte() (I checked in the debugger and it says image() is 7300 bytes long at this point) So why is it doing this? I've tried everything I could think of but either it's too late at night, or I'm still to new at this;) Any Ideas?

Code:
If System.IO.File.Exists(path) Then
   Dim br As New System.IO.BinaryReader(System.IO.File.OpenRead(path))
   Dim image As Byte() = br.ReadBytes(br.BaseStream.Length)
   br.Close()
   image = ByteDecrypt(image, rij) 'home baked function
   picBox.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(image, 0, image.Length))

Thanks,
Dan
 
Last edited:
This is probably the blind leading the blind, but shouldn't the second line read
Visual Basic:
   Dim image() As Byte = br.ReadBytes(br.BaseStream.Length)
instead of
Visual Basic:
   Dim image As Byte() = br.ReadBytes(br.BaseStream.Length)
?

I'm new to .NET as well, so I've probably got the cat by the you know what.:D
 
You might have a point there. I'm going to try that, usually I do Dim arrays as array() as int, that was a mistake.
 
Actually it doesn't seem to be the stream that is the problem, the exception is coming from system.drawing and it seems to be that the memory stream is a bad param for the Image.FromStream() function.
 
Yes you're right, I removed it and it works, so see if you can tell me what's wrong with it, because now I'm completely stumped.

Code:
    Public Function ByteEncrypt(ByVal b As Byte(), ByRef Rij As RijndaelManaged) As Byte()
        Return Rij.CreateEncryptor(rijKey, rijIv).TransformFinalBlock(b, 0, b.Length)
    End Function

    Public Function ByteDecrypt(ByVal b As Byte(), ByRef Rij As RijndaelManaged) As Byte()
        Return Rij.CreateDecryptor(rijKey, rijIv).TransformFinalBlock(b, 0, b.Length)
    End Function

The encrpyt was used to write the file and the decrypt to decrypt it.

The second param passed to both functions was:
Private rij As New System.Security.Cryptography.RijndaelManaged()

Thanks a lot!
-Dan
 
Back
Top