Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Argument Exception when using Image.FromStream(MemoryStream)

Status
Not open for further replies.

dafyddg

Programmer
Dec 2, 2003
92
0
0
GB
I've got a method that i use to convert a byte array into a System.Drawing.Image. The code looks like this:

Code:
private Image ConvertByteArrayToImage(byte[] bytes)
{		
	Image image = new Image();

	using (MemoryStream ms = new MemoryStream(bytes, true))
	{
		ms.Write(bytes, 0, bytes.Length);	
		image = Image.FromStream(ms);
	}

	return image;
}

The byte array is loaded from an SQL database. The byte array that was populated into the database was created by the following method:

Code:
private byte[] ConvertImageToByteArray(Image image)
{
	//Extract the inner image.
	
	byte[] bytes = null;

	using (MemoryStream ms = new MemoryStream())
	{
		image.Save(ms, image.RawFormat);
		bytes = new byte[ms.Length];
		ms.Read(bytes, 0, (int)ms.Length);
	}
	return bytes;
}

When i run the first method and try to recreate the image i get the following exception:

Code:
System.ArgumentException: Invalid parameter used.
   at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
   at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromStream(Stream stream)
   at VehicleImageDALC.ConvertByteArrayToImage(Byte[] bytes) in c:\sourcesafe databases\vehicleimagedalc.cs:line 244

The image that is being loaded is a JPEG.

I'm really not sure where to go next with this problem so any suggestions would be as welcome as actual solutions.

Thanks

Dafydd
 
What line does it fail at? The exception implies that one of your parameters is wrong...

 
Sorry, stupid of me to forget to mention that.

It fails on the following line in ConvertByteArrayToImage method:

Code:
image = Image.FromStream(ms);
 
What type of image is it? TIFF, BMP, JPG? I'm guessing that it might be a format that the Image control doesn't support.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
No, its a JPEG, and i used the Image class to load the file initially so it must be supported. The odd thing is i've seen similar code to exactly the same thing and based my code on that, yet i get the error and they don't.
 
When using FromStream() you must keep the ms object open during the life of the Image object.
-obislavu-
 
Does it work if you use a different image (a smaller one, or a bitmap?)
 
Changing the image does nothing.

Something else thats quite interesting that i noticed while playing with this. If i use the original MemoryStream and pass it to the FromStream method, the image that results is actually bigger than the original image!

I've tried modyfing it to not close the memory stream but that has no effect.

Can anybody suggest another method of doing what i'm trying to do here. Bear in mind that i'm going to be starting with an Image object, have to persist it a database, and then end up with the same Image object?
 
OK, so its not your image thats the issue, but I'd be concerned if I wrote out 1kb of image data and by the time I got it back it was 1.5kb of data...

Maybe the data is getting corrupt and so is no longer a valid jpg?


k

(P.S. what happens if you write your data out to the database, then cut / paste that data into a temporary binary file as a jpg and then open it ?)
 
Well i can confirm that the data isn't been corrupted. I ran some tests which compared the byte array byte by byte, both before it was persisted to the database, and after it was loaded off the database, they matched exactly.

The difference in size appears to be be caused by the FromStream method.

I've also recently run the program on my PC at home and the size came out the same.

I must be missing something somewhere.
 
How much data is FromStream adding? (i.e. is it doubling it?)

And do you realise that you are using 2 different types of memory stream to read and write (the constructors are different, enabling one to be a dynamic size and one to be a static size Im guessing - Cant be sure, I have no docs on the machine Im using today.

Only other thing I can suggest is to put a while loop, or a number of steps in there, and read in the image say 1 byte at a time (or create lots of 1 byte images) and see if the data is coming in concurrently, or if it is say image byte, padded byte, image byte, padded byte etc)

IIRC, in C++ when dealing with images in memory there was a problem with boundaries, i.e. if your image width or height wasnt in a multiple of 16, it used to get padded.

K

Hth, K
 
I have figured out the problem so i'll share it with you for future reference. Basically when i created the initial byte[] from the memory stream, i wasn't resetting the memory stream position to position 0. Therefore the bytes array was filled with '0's which prevented the Image.FromStream from decoding anything.

Taken me over a day to figure this one out.

Code:
private Image ConvertByteArrayToImage(byte[] bytes)
{        
    Image image = new Image();

    using (MemoryStream ms = new MemoryStream(bytes, true))
    {
	[b]myMemoryStream.Position = 0;[/b]
	
        ms.Write(bytes, 0, bytes.Length);    
        image = Image.FromStream(ms);
    }

    return image;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top