Read / Write an Image file
-
I'm having problems trying to accomplish a certain task. I want to save some custom information about an image and then save the image itself in the same file. I can write and read my infomation by itself using a binary writer/reader but as soon as I try to throw a
System.Drawing.Image
in the mix things get messed up. Am I going about using the image correctly like this:... System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Image.Save(iStream, System.Drawing.Imaging.ImageFormat.Png); ...
It seems to me that as soon as I do the above code the filestream gets all weird and I it's no longer in a readable format. Please help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't. -
I'm having problems trying to accomplish a certain task. I want to save some custom information about an image and then save the image itself in the same file. I can write and read my infomation by itself using a binary writer/reader but as soon as I try to throw a
System.Drawing.Image
in the mix things get messed up. Am I going about using the image correctly like this:... System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Image.Save(iStream, System.Drawing.Imaging.ImageFormat.Png); ...
It seems to me that as soon as I do the above code the filestream gets all weird and I it's no longer in a readable format. Please help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.Rob Tomson wrote: System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Why do you Append? Try Create or Truncate.
Work hard and a bit of luck is the key to success.
You don`t need to be genius, to be rich.
-
Rob Tomson wrote: System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Why do you Append? Try Create or Truncate.
Work hard and a bit of luck is the key to success.
You don`t need to be genius, to be rich.
I was doing
Append
because the code before this writes my custom information that I want before the actual image. If I just doCreate
then it would overwrite all the info I just wrote. I've also tried just writing my stuff then writing the image through oneFileStream
but I get the same effect of not being able to read the file afterwards. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't. -
I was doing
Append
because the code before this writes my custom information that I want before the actual image. If I just doCreate
then it would overwrite all the info I just wrote. I've also tried just writing my stuff then writing the image through oneFileStream
but I get the same effect of not being able to read the file afterwards. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.You cant do that, it break the file format. Alternatively, it you wanna encapsulate the image files, you can embed them in the Stream, but you will have to setup (preferably another) Stream to supply only the correct offsets for Image.FromStream() to work. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
You cant do that, it break the file format. Alternatively, it you wanna encapsulate the image files, you can embed them in the Stream, but you will have to setup (preferably another) Stream to supply only the correct offsets for Image.FromStream() to work. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
How would I go about doing that? Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.
-
How would I go about doing that? Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.
Well, if I wanted to save an Image along with additional info, I would (as I am using NTFS) use alternate data streams to store my info. This means, any Image-Processor can still load that file (if the image is in a common format) and my program could utilize the additional data streams attached to the image file. If you really need to save everything into one stream, try writing an offset first. This means: Save into the very first bytes of your file (lets say: the first 4 bytes) an integer which contains the exact location at which the image data begins. So: you format your information (be sure to have some kind of delimiter or such) and store it into a string. Get the length of that string, add 4 and save that integer into your file (BINARY format, not as a string). Then, append your info-string and the image-data. That way you will be able to jump to the image data directly, without having to parse the info first. Thats all. Sid
-
Well, if I wanted to save an Image along with additional info, I would (as I am using NTFS) use alternate data streams to store my info. This means, any Image-Processor can still load that file (if the image is in a common format) and my program could utilize the additional data streams attached to the image file. If you really need to save everything into one stream, try writing an offset first. This means: Save into the very first bytes of your file (lets say: the first 4 bytes) an integer which contains the exact location at which the image data begins. So: you format your information (be sure to have some kind of delimiter or such) and store it into a string. Get the length of that string, add 4 and save that integer into your file (BINARY format, not as a string). Then, append your info-string and the image-data. That way you will be able to jump to the image data directly, without having to parse the info first. Thats all. Sid
That sounds very interesting, thank you. What do you mean by 'use alternate data streams'? Does this mean I can embedd info in the file and still have other programs read the file? That would really helpful if I could do that instead. Any suggestions? Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
-
That sounds very interesting, thank you. What do you mean by 'use alternate data streams'? Does this mean I can embedd info in the file and still have other programs read the file? That would really helpful if I could do that instead. Any suggestions? Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
Well, try googling for "alternate data stream NTFS" and you should find plenty of information. Cheers Sid
-
Well, try googling for "alternate data stream NTFS" and you should find plenty of information. Cheers Sid
wow, thank you very much. I didn't know something like this existed and it's very interesting. Looks like this is what I'm going to use. Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.
-
wow, thank you very much. I didn't know something like this existed and it's very interesting. Looks like this is what I'm going to use. Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.
Just do not forget that this only works on recent NTFS versions. And that it is quite easy to break the alternate streams. I suggest you do some "experiments" by hand first. Cheers Sid