binary write problem.

  • Thread starter Thread starter PCH
  • Start date Start date
P

PCH

I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a file,
the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.

I have the binary data in a byte array and loop through it to write it out.

When i look at the data i can see the characters are different. Like the
conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes);

w.Write(System.Convert.ToChar(bytes));

}

w.Close();

fs.Close();
 
PCH said:
I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a file,
the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.

I have the binary data in a byte array and loop through it to write it out.

When i look at the data i can see the characters are different. Like the
conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes);

w.Write(System.Convert.ToChar(bytes));

}

w.Close();

fs.Close();



What is w, a TextWriter?

Why on earth are you converting each byte to a char? A char is a 2-byte
unicode character.

Just write the byte array to a new IO.FileSteam object.

David
 
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);



David Browne said:
PCH said:
I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a file,
the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.

I have the binary data in a byte array and loop through it to write it out.

When i look at the data i can see the characters are different. Like the
conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes);

w.Write(System.Convert.ToChar(bytes));

}

w.Close();

fs.Close();



What is w, a TextWriter?

Why on earth are you converting each byte to a char? A char is a 2-byte
unicode character.

Just write the byte array to a new IO.FileSteam object.

David
 
PCH said:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);

If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.
 
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").Replace(" ","_") + ".jpg");

sw.Write (bytes);

sw.close();



Jon Skeet said:
PCH said:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);

If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.
 
As far as I know StreamWriter is meant for text. I think a FileStream is
what you are looking for.

PCH said:
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").Replace(" ","_") + ".jpg");

sw.Write (bytes);

sw.close();



Jon Skeet said:
PCH said:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);

If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.
 
PCH said:
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").Replace(" ","_") + ".jpg");

No, StreamWriter is *not* what you want - again, that's talking about
characters.
sw.Write (bytes);


That would have written a single byte - again, not what you want.

Just create a FileStream, and call Write on it.
 
I think i got it!

fs.Write((byte[]) bytes,ipos,bytes.Length -ipos);

image is saved correctly.

Thanks for the help!



Jon Skeet said:
PCH said:
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").Replace(" ","_") +
".jpg");

No, StreamWriter is *not* what you want - again, that's talking about
characters.
sw.Write (bytes);


That would have written a single byte - again, not what you want.

Just create a FileStream, and call Write on it.
 
Back
Top