Funny you should ask this, I just did it the other day. In my case, I'm
storing a bunch of thumbnails in one file and then their offsets in an index
file. So if the file with the images has 20 images, the index file will have
20 offsets.
For writing the files, I create a MemoryStream. Then I write the image to
the memory stream. For example:
MemoryStream ms = new MemoryStream()
image.Save(ms, ImageFormat.Jpe g)
Then get the buffer, and in my case, I append it to the end of a file. I
also write out the image size before I write the image itself. I did that as
follows. The "offset" variable has the location in the file where the image
is stored. I then placed this in a separate index file.:
byte[] buffer = ms.GetBuffer();
long offset = outfile.Seek(0, SeekOrigin.End) ;
BinaryWriter writer = new BinaryWriter(ou tfile);
// Write the length of the image data.
writer.Write((i nt)ms.Length)
// Write the image data
writer.Writer(b uffer, 0, (int) ms.Length)
outfile.Flush() ;
You need to use ms.Length as the buffer will probably be larger than the
actual data. The buffer is grown in chunks, and only regrown as it gets
full.
For reading the file back, you basically do the opposite. Offset is the
location we retrieved as offset above:
infile.Seek(off set, SeekOrigin.Begi n);
BinaryReader reader = new BinaryReader(in file);
// Get the size of the image in bytes
imageDataSize = reader.ReadInt3 2();
// Read in the buffer
byte[] buffer = reader.ReadByte s(imageDataSize );
// Make a memory stream
MemoryStream ms = new MemoryStream(bu ffer)
// Create image from stream
Image img = Image.FromStrea m(ms)
It's all quite simple.
Pete
"S.Creek" <od****@gmail.c om> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
Hi,
I need to take few files (images in my case) and create one file out of
them, this file should be accessed so i can grab a single image from it
if necessary,
i thought of taking all the images and put them in one binary file, but
couldn't figure out a way to do that, anyone has an idea?
thanks
S.Creek