Saving several images to one file

  • Thread starter Thread starter Peder Y
  • Start date Start date
P

Peder Y

Say I have several images, a jpg a gif and a bmp.

Now, I want to save these files to a single binary file.

I store the images in, say, Image img1, img2, img3;

1) How can I retrieve the size of an image in an Image object?

I have a BinaryWriter bw.
I plan to write to a stream writing TYPE (3 char def. like 'B''M''P'),
SIZE (UInt64), and IMAGEDATA.

2) Can I do this using img1.saveFile(bw.BaseStream, ...)?


Thanks!

- Peder -
 
Hello

Assuming that you use a Steam that support seek operations (such as
FileStream) you can do the following

long startPos = bw.BaseStream.Position;
bw.Write((Uint64)0); // placeholder
bw.Write(type); // type can be string, byte array or char array
long imageStartPos = bw.BaseStream.Position;
img1.saveFile(bw.BaseStream, ...);
long endPos = bw.BaseStream.Position;
long imageSize = endPos - imageStartPos;
bw.BaseStream.Position = startPosition;
bw.Write(imageSize);
bw.BaseStream.Position = endPos;

if the Stream doesn't support seek operations, you can save the image to a
memory stream, get its size then write to the original stream.

Best regards,
Sherif
 
Hi!
Thanks! This is my current approach as well, but I haven't coded the
retrieve part yet so it remains to see if it works. My notch, though,
is that I'm not exactly sure what Image.Save/Image.SaveFile saves, and
wether it can be retrieved again from the same stream. One could guess
Image.FromFile uses the file extension to determine how to handle the
stream of bytes, but Image.FromStream cannot retrieve this kind of
information. My hope is thus that this function examines the possible
"header" constellations in the stream and determines its original
format from this. Otherwise, I might have a problem... :-)

- Peder -
 
Back
Top