Delphi - 从 TImageList 中获取位图

4

我正在像这样向 imagelist 添加图像 - 使用 Delphi XE 在运行时添加 png 图像到 imagelist。问题出现在从此列表获取位图并将其保存到硬盘上。

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

这在很多白色bmp文件和一些带有“image”的文件中都会出现。它应该非常容易解决,但我不知道哪里出了问题。
注:以下示例类似于伪代码。下面是代码:
填充列表。
   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;

加载位图的问题

 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;

在问题关闭后进行编辑:请多投一些反对票!谢谢。

2
也许是我自己的问题,但我找不到你提到“问题”的地方。 - Sertac Akyuz
2
首先,为SaveToFile选择一个文件名会很好。 - Tony Hopkinson
2
请发布真实的代码;这段代码无法编译,因为没有不需要文件名的TBitmap.SaveToFile函数。而且您实际上也没有提出问题。 - Ken White
关闭是出于什么原因?问题已被编辑以呈现整个问题。 - RBA
1
@RBA - 有时候当问题被编辑时已经太晚了,一旦它有关闭投票,人们可能会继续投票而不进行广泛的审查。顺便说一句,在我看来,目前你的编辑并没有很明显地解决问题,问题在代码片段的评论中说明,人们必须滚动才能看到它(创建位图,但是它是白色的)。 - Sertac Akyuz
2个回答

7

根据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;

现在位图已正确保存。

5

如果您能提供一些无法正常工作的图像示例,那肯定会有所帮助。同时,您可以尝试使用以下代码进行操作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接