Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

 

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

Edited by Eloff
Posted

This is probably the blind leading the blind, but shouldn't the second line read

  Dim image() As Byte = br.ReadBytes(br.BaseStream.Length)

instead of

  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

Posted
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.
Posted

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.

 

   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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...