views:

52

answers:

4

Hi, there a way to determine that I am passing the right byte array to the MemoryStream if I want to create an Image out of byte array.

MemoryStream mStream = new MemoryStream();
mStream.Write(byteArray, 0, byteArray.Lenth);
Image imgObj = Image.FromStream(mStream);

How can I, if possible Correct the byteArray that it is a valid byteArray for an Image?

A: 

This is a really ominous question, surely you must know where you are reading your data from? When you create an image using Image.FromStream, an ArgumentException will be thrown if it cannot recognise the format. Why don't you use that mechanism for identifying an incorrect stream of data, rather than re-invent the wheel?

Matthew Abbott
yes I know where I'm reading my data from but I'm trying to manipulate (add some and get some) the byte that was generated out from it, therefore I get the `ArgumentException` but is it possible to Correct the byteArray?
rob waminal
To be honest, without an extensive knowledge of how the byte array is corrupted, I can't see how you can fix it. This of course will vary from format to format. You'd need some sort of framework/application that can analyse that byte array itself, not something I think is easily accomplished....
Matthew Abbott
@Matthew, ok thanks for the info.
rob waminal
A: 

I've done a bit of programatic image manipulation myself. The thing you'll want to do is find the spec for the image format that you are modifying and make sure you do everythign you should. For example png files are chunked and have checksums on each section so if you change something in that chunk you have to recalculate the checksum at the end of the section.

Chris
A: 

After reading your questions and your comments, i think what you're trying is to manipulate the image by manipulating the byte array before you put it into the Image class. And now you claim that your byte array is corrupt for this image format and how you can correct it.

So the answer to this question would be: You corrupted it, you'll fix it.

But to really solve your problem, if your goal is to manipulate the picture itself, just load it into an interims Image and use the Graphics class to manipulate your picture. Afterwards put the result into the real image object you like. Ready, without any hassle about working on the byte array.

Oliver
I found a solution to it. I read about this article `Image Processing for Dummies with C# and GDI+` it directly manipulates the byte via pointer using GDI+. Thanks for the tip Oliver
rob waminal
+1  A: 

here's the answer Image Processing for Dummies with C# and GDI+

OT: i don't know how to put links on comments so I put it in the answers.

rob waminal
a comment follows the same syntax as for a normal question. But instead of using Basic Links you have to take the Advanced Link version from the [documentation](http://stackoverflow.com/editing-help). So just write `[Image Processing ...](http://...)`
Oliver