Author: Tomas Rutkauskas
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.
1 2 procedure SaveBitmapToStream(aBitmap: TBitmap; aStream: TStream);
3 var4 ms: TMemoryStream;
5 size: Integer;
6 begin7 Assert(Assigned(aBitmap));
8 Assert(Assigned(aStream));
9 ms := TMemoryStream.Create;
10 try11 aBitmap.SaveToStream(ms);
12 ms.position := 0;
13 size := ms.Size;
14 aStream.WriteBuffer(size, Sizeof(size));
15 aStream.CopyFrom(ms, size);
16 finally17 ms.free
18 end;
19 end;
20 21 //then22 23 aStream.SaveToFile('FileName');
24 25 //to read then first off do:26 27 aStream.LoadFromFile('FileName');
28 29 //then30 31 procedure LoadBitmapFromStream(aBitmap: TBitmap; aStream: TStream);
32 var33 ms: TMemoryStream;
34 size: Integer;
35 begin36 Assert(Assigned(aBitmap));
37 Assert(Assigned(aStream));
38 ms := TMemoryStream.Create;
39 try40 aStream.ReadBuffer(size, Sizeof(size));
41 ms.CopyFrom(aStream, size);
42 ms.position := 0;
43 aBitmap.LoadfromStream(ms);
44 finally45 ms.free
46 end;
47 end;