Search code examples
delphidelphi-xe

Delphi - get bitmap from a TImageList


I'm adding an image to an imagelist like here - Add a png image to a imagelist in runtime using Delphi XE. The problem occurs when getting an bitmap from this list and saving it to the hard drive.

bmp:=tbitmap.create;
imagelist.getbitmap(0,bmp);
bmp.savetofile()

this occurs in a lot of white bmp files and several with 'image'. it should be extremely easily but I can not understand what is wrong.

LE: the example was more as pseudo-code.code bellow:

filling the list

   FImageList := TImageList.Create(nil);
   FImageList.Masked:=false;
   FImageList.ColorDepth:=cd32bit;
   FImageList.SetSize(32,32);//I am sure that all images are 32x32
   while not dsTemp.eof do//dstemp is a Tdatasetdescendant
    begin
     ststream := dsTemp.CreateBlobStream(dsTemp.FieldByName('FLAG'), bmRead);

     pngImage := TPngImage.Create;
     pngImage.LoadFromStream(ststream);

     btBitmap := TBitmap.Create;
     btBitmap.PixelFormat := pf32bit;
     btBitmap.Width := pngImage.Width ;
     btBitmap.Height := pngImage.Height ;
     pngImage.AssignTo(btBitmap);
     btBitmap.AlphaFormat:=afIgnored;

     res := FImageList.Add(btBitmap,nil);
//     pngImage.savetofile('C:\a\'+inttostr(res)+'.png');-works. image is ok
//     btBitmap.savetofile('C:\a\'+inttostr(res)+'.bmp');-works. image is ok
     dsTemp.Next;
     freeandnil(btBitmap);
     freeandnil(pngImage);
    end;

the problem with loading the bitmap

 for iPos := 0 to FImageList.Count-1 do
  begin
     btBitmap := tbitmap.create;
     FImageList.GetBitmap(iPos,btBitmap);
     btBitmap.savetofile('C:\a\'+inttostr(iPos)+'thr.bmp');//creates the bitmap, but it is white
  end;

Edit after the question was closed: more downvotes please! Thanks


Solution

  • It would definitely help if you could give an example for the images that dont't work. Meanwhile you can try playing around with this code:

    bmp.PixelFormat := pf32bit;
    bmp.AlphaFormat := afDefined;
    ImageList.GetBitmap(0, bmp);