Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2004, 06:05 PM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default 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
Reply With Quote
  #2  
Old 07-20-2004, 02:01 AM
digitiger digitiger is offline
Senior Member
 
Join Date: Jan 2001
Posts: 716
Default RE: Save/Load TImageList to/from a file ...

just break down your image list into multiple smaller lists
Reply With Quote
  #3  
Old 07-20-2004, 02:32 AM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default 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
Reply With Quote
  #4  
Old 07-20-2004, 02:33 AM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default 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
Reply With Quote
  #5  
Old 07-20-2004, 05:39 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default 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
Reply With Quote
  #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
  #7  
Old 07-20-2004, 05:27 PM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default 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
Reply With Quote
  #8  
Old 07-20-2004, 05:28 PM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default 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
Reply With Quote
  #9  
Old 08-09-2004, 09:40 PM
sammy73 sammy73 is offline
Junior Member
 
Join Date: Aug 2004
Posts: 1
Default 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.
Reply With Quote
  #10  
Old 08-10-2004, 04:41 AM
Raver Raver is offline
Senior Member
 
Join Date: May 2001
Posts: 525
Default RE: Save/Load TImageList to/from a file ...

thanx for the kind answer


Kind Regards
R A V E R
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:21 PM.


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