4

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

Community
  • 1
  • 1
RBA
  • 12,337
  • 16
  • 79
  • 126
  • 2
    Maybe it's me, but I cannot find the place where you mention the *problem*. – Sertac Akyuz Nov 12 '12 at 22:27
  • 2
    Well straight off, a file name for SaveToFile would be good. – Tony Hopkinson Nov 12 '12 at 22:53
  • 2
    Please post real code; this won't compile, as there is no `TBitmap.SaveToFile` that works without a filename. You also haven't actually asked a question. – Ken White Nov 13 '12 at 00:58
  • Closed for what reason? Question was edited to present the entire problem. – RBA Nov 13 '12 at 10:40
  • 1
    @RBA - Sometimes it is too late when the question is edited, once it has close votes people may continue to vote without extensive examination. BTW IMHO, currently your edit does not make the problem quite obvious, the problem is stated in a comment in a code snippet and one has to scroll to see it (*creates the bitmap, but it is white*). – Sertac Akyuz Nov 13 '12 at 17:06

2 Answers2

6

based on Uwe Raabe's answer I make it work.Solution:

 for iPos := 0 to FImageList.Count-1 do
  begin
     btBitmap := tbitmap.create;
     btBitmap.PixelFormat := pf32bit;
     btBitmap.AlphaFormat := afIgnored;
     FImageList.GetBitmap(iPos,btBitmap);
     btBitmap.savetofile('C:\a\'+inttostr(iPos)+'thr.bmp');
  end;

now the bitmaps are saved correctly.

Community
  • 1
  • 1
RBA
  • 12,337
  • 16
  • 79
  • 126
5

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);
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130