Mirror

How to save several TBitmaps into one file (Views: 26)


Problem/Question/Abstract:

Does anybody know whether it is possible to write some small TBitmaps with different widths and heights into one file?

Answer:

Saving the TBitmap to a Stream, and appending other TBitmaps to that stream, then saving the stream to disk would be the method.

procedure SaveBitmapToStream(aBitmap: TBitmap; aStream: TStream);
var
  ms: TMemoryStream;
  size: Integer;
begin
  Assert(Assigned(aBitmap));
  Assert(Assigned(aStream));
  ms := TMemoryStream.Create;
  try
    aBitmap.SaveToStream(ms);
    ms.position := 0;
    size := ms.Size;
    aStream.WriteBuffer(size, Sizeof(size));
    aStream.CopyFrom(ms, size);
  finally
    ms.free
  end;
end;

then

aStream.SaveToFile('FileName');

to read then first off do:

aStream.LoadFromFile('FileName');

then

procedure LoadBitmapFromStream(aBitmap: TBitmap; aStream: TStream);
var
  ms: TMemoryStream;
  size: Integer;
begin
  Assert(Assigned(aBitmap));
  Assert(Assigned(aStream));
  ms := TMemoryStream.Create;
  try
    aStream.ReadBuffer(size, Sizeof(size));
    ms.CopyFrom(aStream, size);
    ms.position := 0;
    aBitmap.LoadfromStream(ms);
  finally
    ms.free
  end;
end;

<< Back to main page