views:

43

answers:

2

I have a method which returns a array of ByteArray:

public byte[][] Draw(ImageFormat imageFormat, ImageSize imageSize);

and I need to write it into a MemoryStream:

var byteArray = instanceName.Draw(ImageFormat.Jpeg, ImageSize.Dpi150);
MemoryStream ms = new MemoryStream(byteArray[0]);

This is working so far because the array of byteArray only ever has one element. Would someone be able to point out and provide a solution on : what would happen if the array of byteArray has more than one element?

I guess with current code I would still take the first element of the byteArray and discard the rest, but I need MemoryStream and it can not take a multi-dimensional array.

+1  A: 

iYou will have to loop and write, something like this:

var ms = new MemoryStream();
for(var i=0; i < byteArray.Length; i++)
  ms.Write(byteArray[i], 0, byteArray[i].Length);

(I'm not sure it's working as-is, you might have to adjust it, but it's the principle)

Onkelborg
This might not work as the second param in `Write` byte offset will not be the 0 for elements other than the first one. Good answer though.
VoodooChild
Hm, no? It's the offset to begin writing from, not to.
Onkelborg
you are right it is the byte offset in buffer at which to begin writing from. So this might just work as is then, no?
VoodooChild
Probably, but I haven't tested it, so there might be compilation errors, or something similar :P
Onkelborg
A: 

As you mentioned only the first element at [0] position would be used by memory stream. Since memory stream is sequential by design, you need a loop to flat your array of arrays and put it into memory stream. As a second look to your code, I suggest to change your Draw() method to produce linear data structure rather than two dimensional (array of array) if you don't need this type of data structure anywhere else in your code.

Xaqron
I can't, this is from a third party control - I thought about that :)
VoodooChild
Then write a wrapper class around it.
Xaqron
Ok, what if there is a reason that the third party control is returning an array of byteArray? Should we write a wrapper class just to change the return types of the original function?
VoodooChild
It depends on how frequently you are going to flat a 2D memory model. If it's frequent, then I would write my wrapper class for encapsulating just ONE method.
Xaqron