View Single Post
  #6  
Old 07-20-2004, 01:56 PM
chris_w chris_w is offline
Senior Member
 
Join Date: Jan 2004
Posts: 1,397
Default RE: Save/Load TImageList to/from a file ...

[pre]
Code:
No idea whether it's faster or not...

procedure SaveImages(Stream: TStream; ImageList: TImageList);
var
  wid, hgt, cnt, sz : longint;
  i, x, y : integer;
  bmp : TBitmap;
  MS : TMemoryStream;
begin
  Stream.Size := 0;
  wid := ImageList.Width;
  Stream.Write(wid, SizeOf(wid));

  hgt := ImageList.Height;
  Stream.Write(hgt, SizeOf(hgt));

  cnt := ImageList.Count;
  Stream.Write(cnt, SizeOf(cnt));

  if cnt > 0 then begin
    bmp := TBitmap.Create;
    try
      x := wid *cnt;
      if x > Screen.Width then begin
        bmp.Width := wid *(Screen.Width div wid);
        bmp.Height := hgt *((x div bmp.Width) +1);
      end else begin
        bmp.Width := x;
        bmp.Height := hgt;
      end;

      x := 0;
      y := 0;
      for i := 0 to cnt -1 do begin
        ImageList.Draw(bmp.Canvas, x, y, i);

        Inc(x, wid);
        if x >= bmp.Width then begin
          x := 0;
          Inc(y, hgt);
        end;
      end;

      MS := TMemoryStream.Create;
      try
        bmp.SaveToStream(MS);

        sz := MS.Size;
        Stream.Write(sz, SizeOf(sz));
        Stream.CopyFrom(MS, 0);
      finally
        MS.Free;
      end;

    finally
      bmp.Free;
    end;
  end;
end;

procedure LoadImages(Stream: TStream; ImageList: TImageList);
var
  bmp, mask, image : TBitmap;
  wid, hgt, cnt, sz : longint;
  MS : TMemoryStream;
  x, y : integer;
begin
  Stream.Position := 0;

  Stream.Read(wid, SizeOf(wid));
  Stream.Read(hgt, SizeOf(hgt));
  Stream.Read(cnt, SizeOf(cnt));

  ImageList.Clear;
  ImageList.Height := hgt;
  ImageList.Width := wid;

  if cnt > 0 then begin
    ImageList.AllocBy := cnt;

    bmp := TBitmap.Create;
    image := TBitmap.Create;
    mask := TBitmap.Create;
    try
      Stream.Read(sz, SizeOf(sz));
      MS := TMemoryStream.Create;
      try
        MS.CopyFrom(Stream, sz);
        MS.Position := 0;
        bmp.LoadFromStream(MS);
      finally
        MS.Free;
      end;

      mask.Width := wid;
      mask.Height := hgt;

      image.Width := wid;
      image.Height := hgt;

      x := 0;
      y := 0;
      while cnt > 0 do begin
        image.Canvas.Draw(-x, -y, bmp);

        mask.Assign(image);        // lower left pixel
        mask.Canvas.Brush.Color := image.Canvas.Pixels[0, hgt -1];
        mask.Monochrome := true;

        ImageList.Add(image, mask);
        Dec(cnt);

        Inc(x, wid);
        if x >= bmp.Width then begin
          Inc(y, hgt);
          x := 0;
        end;
      end;

    finally
      bmp.Free;
      image.Free;
      mask.Free;
    end;
  end;

end;
[/pre]

Chris
Reply With Quote