Delphi - 从TImageList获取位图

RBA*_*RBA 4 delphi delphi-xe

我正在将图像添加到像这里的图像列表中 - 使用Delphi XE在运行时将png图像添加到图像列表.从此列表中获取位图并将其保存到硬盘驱动器时会出现此问题.

bmp:=tbitmap.create;
imagelist.getbitmap(0,bmp);
bmp.savetofile()
Run Code Online (Sandbox Code Playgroud)

这发生在很多白色bmp文件和几个带有"图像"的文件中.它应该非常容易,但我无法理解什么是错的.

LE:这个例子更像伪代码.下面是:

填写清单

   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;
Run Code Online (Sandbox Code Playgroud)

加载位图的问题

 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;
Run Code Online (Sandbox Code Playgroud)

问题结束后编辑:请更多downvotes!谢谢

RBA*_*RBA 6

根据Uwe Raabe的回答,我让它工作.解决方案:

 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;
Run Code Online (Sandbox Code Playgroud)

现在位图已正确保存.


Uwe*_*abe 5

如果你能给出一些不起作用的图像的例子肯定会有所帮助.同时你可以尝试使用这段代码:

bmp.PixelFormat := pf32bit;
bmp.AlphaFormat := afDefined;
ImageList.GetBitmap(0, bmp);
Run Code Online (Sandbox Code Playgroud)