Board index » delphi » Save variable amount of Bitmaps (w/ different sizes) in a single File or Stream

Save variable amount of Bitmaps (w/ different sizes) in a single File or Stream

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

 

Re:Save variable amount of Bitmaps (w/ different sizes) in a single File or Stream


Quote
"Oliver Kessler" <okess...@vme.de> wrote in message news:8kcnqg$f2d1@bornews.borland.com...
> 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 ???

I don't understand what you're trying to do yet.  You save three
BMPs to a stream.  Are you trying to reload these three BMPs
into a single image, or three images?

If you want to combine BMPs into a single BMP you need to
create a new BMP and use something like Draw or StretchDraw
to copy each "source" BMP to a single "destination" BMP.

Please give some more details on what you'd like to do.
--
efg

Earl F. Glynn     E-mail:  EarlGl...@att.net
Overland Park, KS  USA

efg's Computer Lab:  http://www.efg2.com/Lab

Re:Save variable amount of Bitmaps (w/ different sizes) in a single File or Stream


"Earl F. Glynn" <EarlGl...@att.net> wrote in message
news:8kcsg1$f5f9@bornews.borland.com...

Quote
> Please give some more details on what you'd like to do.
> --

Either that, or some indication of what is wrong.
What is your code doing or not doing?

It looks like it should read in three images to your
form, with each overwriting the previous with the
result that it shows the last one.
I don't know the point of that, but I'm sure you have one.
Is that what it was supposed to do?

Brad.

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

Other Threads