Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   Save/Load TImageList to/from a file ... (http://www.delphipages.com/forum/showthread.php?t=117855)

Raver 07-19-2004 06:05 PM

Save/Load TImageList to/from a file ...
 
Hello

Ive used this command to save a TImageList to a file :

WriteComponentResFile('imagelist.dat',ImageList1);

and this command to read the file and load images
into the TImageList :

ImageList1:= ReadComponentResFile('imagelist.dat', nil) as TImagelist;


It works fine , but it has a problem with is
very very slow , since I have a huge list of
images in the ImageList1 I need some fastr
method to save and load the images.

Any idea ?


Kind Regards
R A V E R

digitiger 07-20-2004 02:01 AM

RE: Save/Load TImageList to/from a file ...
 
just break down your image list into multiple smaller lists

Raver 07-20-2004 02:32 AM

RE: Save/Load TImageList to/from a file ...
 
but it will be hundred of files !
I like to save it as a single file !


Kind Regards
R A V E R

Raver 07-20-2004 02:33 AM

RE: Save/Load TImageList to/from a file ...
 
besides thats very slow,
I would like to try a fast one !


Kind Regards
R A V E R

MrBaseball34 07-20-2004 05:39 AM

RE: Save/Load TImageList to/from a file ...
 
Found this on the Borland newsgroups, give it a try...

The following code fragments (D5) save/load ImageLists
to/from stream or file:

[pre]
Uses
CommCtrl;

{...}

Procedure SaveImageListToStream(IL:TImageList;AStream:TStrea m);
Var SAdapter:TStreamAdapter;
Begin
SAdapter:=TStreamAdapter.Create(AStream);
Try
If Not ImageList_Write(IL.Handle,SAdapter) Then Begin
{ error handling ... }
End;
Finally
SAdapter.Free;
End;
End;

Procedure LoadImageListFromStream(IL:TImageList;AStream:TStr eam);
Var SAdapter:TStreamAdapter;
Begin
SAdapter:=TStreamAdapter.Create(AStream);
Try
IL.Handle:=ImageList_Read(SAdapter);
If (IL.Handle=0) Then Begin
{ error handling ... }
End;
Finally
SAdapter.Free;
End;
End;

Procedure SaveImageListToFile(IL:TImageList;Filename:String) ;
Var AStream:TFileStream;
Begin
AStream:=TFileStream.Create(Filename,fmCreate);
Try
SaveImageListToStream(IL,AStream);
Finally
AStream.Free;
End;
End;

Procedure LoadImageListFromFile(IL:TImageList;Filename:Strin g);
Var AStream:TFileStream;
Begin
AStream:=TFileStream.Create(Filename,fmOpenRead);
Try
LoadImageListFromStream(IL,AStream);
Finally
AStream.Free;
End;
End;
[/pre]

MB34

chris_w 07-20-2004 01:56 PM

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

Raver 07-20-2004 05:27 PM

RE: Save/Load TImageList to/from a file ...
 
OMG !!!
did you write these codes just for me ??????
Wow , thanx a million :)
I will give it a try :)
Thanx a lot


Kind Regards
R A V E R

Raver 07-20-2004 05:28 PM

RE: Save/Load TImageList to/from a file ...
 
Thanx a lot
your code is simple and fine ...
I hope it will be fast too :)
Thanx a million


Kind Regards
R A V E R

sammy73 08-09-2004 09:40 PM

RE: Save/Load TImageList to/from a file ...
 
You can also load images from a resource DLL. In order to do that you should:

1. create a resource with your images
2. create a DLL with that resource
3. load DLL from your application
4. copy images from DLL to TImageList
5. unload DLL when your application terminates

In this case it is possible to distribute a DLL file separately. You can also load just the images that you need. If you are interested in more detailed description of the five steps above, just let me know.

Raver 08-10-2004 04:41 AM

RE: Save/Load TImageList to/from a file ...
 
thanx for the kind answer


Kind Regards
R A V E R


All times are GMT. The time now is 07:27 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.