Re:Save variable amount of Bitmaps (w/ different sizes) in a single File or Stream
I have to agree that it isn't clear what you want to do in your example,
but if you're just having trouble with streams and are asking for help on
that, then here's a slightly modified version of your code that should work
correctly in saving several bitmaps to a stream and then reading them back:
procedure TForm1.Button1Click(Sender: TObject);
var
i: Byte;
FileStream: TFileStream;
MemStream: TMemoryStream;
SizeArray: array[0..2] of Longint;
TempBitmap: TBitmap;
begin
FileStream := TFilestream.Create('Test.tmp',fmCreate);
TempBitmap:= TBitmap.Create;
for i := 0 to 2 do begin
case i of
0: TempBitmap.LoadFromFile('C:\Windows\Bubbles.Bmp');
1: TempBitmap.LoadFromFile('C:\Windows\Circles.Bmp');
2: TempBitmap.LoadFromFile('C:\Windows\Tiles.Bmp');
end;
MemStream:= TmemoryStream.Create;
TempBitmap.SaveToStream(MemStream);
SizeArray[i]:= MemStream.Size;
FileStream.CopyFrom(MemStream, 0);
MemStream.Free;
end;
TempBitmap.Free;
FileStream.Free;
FileStream:= TFilestream.Create('Test.tmp',fmOpenRead);
TempBitmap:= TBitmap.Create;
for i:= 0 to 2 do begin
MemStream:= TMemoryStream.Create;
MemStream.CopyFrom(FileStream, SizeArray[i]);
MemStream.Position:= 0;
TempBitmap.LoadFromStream(MemStream);
Canvas.Draw(0, 0, TempBitmap);
Sleep(1000);
Repaint;
MemStream.Free;
end;
TempBitmap.Free;
FileStream.Free;
end;
Quote
Oliver Kessler wrote:
> Hi,
> I am really trying hardly to solve the following problem and
> I have till now no idea. May somebody help me on this ?
> I want to save several (small) Bitmaps (8 bit) with different sizes
> to a stream or a File. After this I want to load them back in an Image.
> What is wrong with my code ???
> Global Var's:
> FileStream : TFileStream;
> MemStream : TMemoryStream;
> SizeArray : Array[0..2] of Longint;
> Procedure Read_From_Stream;
> Var i : byte;
> Begin
> FileStream := TFilestream.Create('Test.tmp',fmOpenRead);
> For i := 0 to 2 do
> Begin
> MemStream := TmemoryStream.Create;
> MemStream.position := 0;
> FileStream.ReadBuffer(MemStream,SizeArray[i]);
> Form1.Image1.Picture.Bitmap.LoadFromStream(MemStream);
> MemStream.Free;
> End;
> FileStream.Free;
> End;
> Procedure Write_To_Stream;
> Var i : byte;
> Begin
> FileStream := TFilestream.Create('Test.tmp',fmCreate);
> For i := 0 to 2 do
> begin
> cse i of
> 0 : TempBitmap.LoadFromFile('Test1.Bmp');
> 1 : TempBitmap.LoadFromFile('Test2.Bmp');
> 2 : TempBitmap.LoadFromFile('Test3.Bmp');
> end;
> MemStream := TmemoryStream.Create;
> TmpBitmap.SaveToStream(MemStream);
> MemStream.position := 0;
> SizeArray[i] := MemStream.size;
> FileStream.WriteBuffer(MemStream,MemStream.size);
> MemStream.Free;
> end;
> FileStream.Free;
> end;
> I would appreciate very much any help.
> Best regards
> Olly