Serializing System.Array HELP!

G

Guest

Hi All -

I've got a 3rd party COM object that returns an array of bytes that are a
TIFF image. After adding the reference to the com object to my solution the
C# signature
for the method is something like this:

System.Array Item.GetContent();

I need to save the bytes to a file that will be the tiff file...so when I
see System.Array I think "serialization". I have code like:

System.Array theBytes = anItem.GetContent();
FileStream fs = new FileStream("image.tif", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, theBytes);
fs.Close();

Problem is, the serialize function adds more stuff to the file than just the
raw data and its no longer a valid TIFF!

The only way I could get it to work was to loop through the array and write
1 byte
at a time but performance is terrible! (And performance *is* an issue!)

Is ther a way to write the contents of System.Array as binary data quickly?
Or optionally, how do I convert System.Array to a byte[] (without looping
through all the data)?

Please help! I'm really stuck!
 
A

Andy Bates

Try the following:

byte[] theBytes = anItem.GetContent();
using (FileStream fs = File.Create("image.tif"))
{
fs.Write (theBytes, 0, theBytes.Length);
fs.Close();
}

The only potential issue is the first line, I'm not sure what the signature
for GetContent is but basically you want it as a byte array (i.e. the
contents of the file).

HTH

- Andy
 
G

Guest

Thanks for the reply...
I did try what you suggested and it wont compile. Error compiling is:
Cannot implicitly convert type 'System.Array' to 'byte[]'



Andy Bates said:
Try the following:

byte[] theBytes = anItem.GetContent();
using (FileStream fs = File.Create("image.tif"))
{
fs.Write (theBytes, 0, theBytes.Length);
fs.Close();
}

The only potential issue is the first line, I'm not sure what the signature
for GetContent is but basically you want it as a byte array (i.e. the
contents of the file).

HTH

- Andy

clawton said:
Hi All -

I've got a 3rd party COM object that returns an array of bytes that are a
TIFF image. After adding the reference to the com object to my solution
the
C# signature
for the method is something like this:

System.Array Item.GetContent();

I need to save the bytes to a file that will be the tiff file...so when I
see System.Array I think "serialization". I have code like:

System.Array theBytes = anItem.GetContent();
FileStream fs = new FileStream("image.tif", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, theBytes);
fs.Close();

Problem is, the serialize function adds more stuff to the file than just
the
raw data and its no longer a valid TIFF!

The only way I could get it to work was to loop through the array and
write
1 byte
at a time but performance is terrible! (And performance *is* an issue!)

Is ther a way to write the contents of System.Array as binary data
quickly?
Or optionally, how do I convert System.Array to a byte[] (without looping
through all the data)?

Please help! I'm really stuck!
 
G

Guest

Duh! I just got it!
Just cast the dumb thing! :)
Thanks again!

clawton said:
Thanks for the reply...
I did try what you suggested and it wont compile. Error compiling is:
Cannot implicitly convert type 'System.Array' to 'byte[]'



Andy Bates said:
Try the following:

byte[] theBytes = anItem.GetContent();
using (FileStream fs = File.Create("image.tif"))
{
fs.Write (theBytes, 0, theBytes.Length);
fs.Close();
}

The only potential issue is the first line, I'm not sure what the signature
for GetContent is but basically you want it as a byte array (i.e. the
contents of the file).

HTH

- Andy

clawton said:
Hi All -

I've got a 3rd party COM object that returns an array of bytes that are a
TIFF image. After adding the reference to the com object to my solution
the
C# signature
for the method is something like this:

System.Array Item.GetContent();

I need to save the bytes to a file that will be the tiff file...so when I
see System.Array I think "serialization". I have code like:

System.Array theBytes = anItem.GetContent();
FileStream fs = new FileStream("image.tif", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, theBytes);
fs.Close();

Problem is, the serialize function adds more stuff to the file than just
the
raw data and its no longer a valid TIFF!

The only way I could get it to work was to loop through the array and
write
1 byte
at a time but performance is terrible! (And performance *is* an issue!)

Is ther a way to write the contents of System.Array as binary data
quickly?
Or optionally, how do I convert System.Array to a byte[] (without looping
through all the data)?

Please help! I'm really stuck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top