Modify Jpeg via BinaryWriter renders "useless" output

R

Richard Coltrane

Hello,

If i take a jpeg and pipe it from a binary reader to a binary writer,
everything is fine and I can still use it.
If i take that same jpeg and piping code and simply add a byte encoded
string to the front of the file then I can no longer open it in an image
editor. This i would expect.

However if i open that image in notebook and remove the string I entered,
then i still cant open the file in an image editor, does anyone know why
this is?? Once ive removed the 4 character string with notepad the files are
"seemingly" exactly the same?? They have precisely the same file size (down
to the byte) and the bytes within the file are also exactly the same??

Whats different?

Thanks
 
J

Jon Skeet [C# MVP]

Richard Coltrane said:
If i take a jpeg and pipe it from a binary reader to a binary writer,
everything is fine and I can still use it.
If i take that same jpeg and piping code and simply add a byte encoded
string to the front of the file then I can no longer open it in an image
editor. This i would expect.

However if i open that image in notebook and remove the string I entered,
then i still cant open the file in an image editor, does anyone know why
this is?? Once ive removed the 4 character string with notepad the files are
"seemingly" exactly the same?? They have precisely the same file size (down
to the byte) and the bytes within the file are also exactly the same??

Whats different?

That's almost certainly due to using notepad rather than anything the
..NET code is doing. When anything tries to interpret arbitrary binary
data as text it's very easy to lose information.

Take the .NET code out of the equation by doing the same steps manually
with your text editor and you're likely to see the exact same
behaviour. If you don't, we'd need to see the actual code involved.
 
H

Hans Kesting

Richard Coltrane formulated on zondag :
Hello,

If i take a jpeg and pipe it from a binary reader to a binary writer,
everything is fine and I can still use it.
If i take that same jpeg and piping code and simply add a byte encoded string
to the front of the file then I can no longer open it in an image editor.
This i would expect.

However if i open that image in notebook and remove the string I entered,
then i still cant open the file in an image editor, does anyone know why this
is?? Once ive removed the 4 character string with notepad the files are
"seemingly" exactly the same?? They have precisely the same file size (down
to the byte) and the bytes within the file are also exactly the same??

Whats different?

Thanks

Try using your own process in reverse: use a .Net program to strip
those first bytes and leave the rest of the data intact.
I suspect that that *will* work (see the other replies)

Hans Kesting
 
R

Richard Coltrane

Hi there,

You're right.

But if i do a byte by byte comaprision the files (the original vrs the
modified after i use notepad to take out the test string and then save the
file) they are still the same?
So if i was removing or altering something other than the 4 byte test string
surely the file contents would be different.

Is this something to do with the os file system itself?? Other than
partition types etc, I didn't realise the file system actually "defined" the
file. I thought the whole idea of the file was that it encapsulated its own
meaning??

Thanks
 
R

Richard Coltrane

Hi Peter,

no it wasn't but thanks for the response, ive always wonderd what that was
called. Spies and school kids use that stuff right?
 
J

Jon Skeet [C# MVP]

Richard Coltrane said:
You're right.

But if i do a byte by byte comaprision the files (the original vrs the
modified after i use notepad to take out the test string and then save the
file) they are still the same?

How are you doing the comparison?
So if i was removing or altering something other than the 4 byte test string
surely the file contents would be different.

Yes, and I suspect you'll find they *are* different.
Is this something to do with the os file system itself?? Other than
partition types etc, I didn't realise the file system actually "defined" the
file. I thought the whole idea of the file was that it encapsulated its own
meaning??

No, I suspect it's more likely to be to do with opening the file in
Notepad, and the way you're comparing the two files.

If you've got two files with the same contents and file A opens up fine
as a JPEG, then I would expect file B to open fine likewise.
 
R

Richard Coltrane

Hi there,

Im using a couple of binary reader to loop through each byte in each file in
sequential order and doing a compare on each byte.... although i suspect a
simple hash would work just as well.

Reversing the process as per Hans suggestion gives me back a useable Jpeg
which in turn supports your idea that its notepad... which makes sense....
but i cant find the "difference" between the two files.

Thanks
 
J

Jon Skeet [C# MVP]

Richard Coltrane said:
Im using a couple of binary reader to loop through each byte in each file in
sequential order and doing a compare on each byte.... although i suspect a
simple hash would work just as well.

Reversing the process as per Hans suggestion gives me back a useable Jpeg
which in turn supports your idea that its notepad... which makes sense....
but i cant find the "difference" between the two files.

That suggests that your comparison code is broken. Could you post it?
 
L

Lasse Vågsæther Karlsen

Richard said:
Hi there,

Im using a couple of binary reader to loop through each byte in each file in
sequential order and doing a compare on each byte.... although i suspect a
simple hash would work just as well.

Reversing the process as per Hans suggestion gives me back a useable Jpeg
which in turn supports your idea that its notepad... which makes sense....
but i cant find the "difference" between the two files.

Thanks
<snip replies from Jon>

Try using the command line file comparison tool built into Windows:

fc /B a.jpg b.jpg

The /B is just to make sure you're using a binary comparison, since I do
not know the heuristics involved in determining if the files are text.
 

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