Eloff Posted April 2, 2003 Posted April 2, 2003 (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 April 2, 2003 by Eloff Quote
me_again Posted April 2, 2003 Posted April 2, 2003 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 Quote
Eloff Posted April 2, 2003 Author Posted April 2, 2003 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. Quote
Eloff Posted April 2, 2003 Author Posted April 2, 2003 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. Quote
*Gurus* divil Posted April 2, 2003 *Gurus* Posted April 2, 2003 I guess your "home baked function" is making the image unreadable. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Eloff Posted April 3, 2003 Author Posted April 3, 2003 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 Quote
Eloff Posted April 3, 2003 Author Posted April 3, 2003 Hey nice page btw, *downloads all the controls* those will come in handy! Thanks! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.