Question

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?

Was it helpful?

Solution 4

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.

OTHER TIPS

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?

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top