Delphi - 从 TImageList 获取位图

标签 delphi delphi-xe

我正在将图像添加到图像列表,如下所示 - Add a png image to a imagelist in runtime using Delphi XE 。从该列表获取位图并将其保存到硬盘驱动器时会出现问题。

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

这种情况发生在很多白色 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;

加载位图的问题

 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;

问题结束后编辑:请多投反对票!谢谢

最佳答案

基于Uwe Raabe's答案我让它工作。解决方案:

 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;

现在位图已正确保存。

关于Delphi - 从 TImageList 获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13352497/

相关文章:

delphi - 通过注册表而不是 TLB 注册应用程序

delphi - 使 TChromium 渲染抗锯齿

delphi - 为什么 TObjectList<T>.Clear 不释放对象?

sql - 如何监控我的 Delphi 应用程序执行的 SQL?

delphi - 组合匿名过程和嵌套过程时出现错误代码

delphi - 如何捕获并打开拖放到应用程序图标上的文件?

delphi - 防止delphi重绘列

delphi - 如何使用外部字体?

delphi - 在 Delphi 5 中自动读取 Excel 并运行 Excel 2007

delphi - 如何获取内存中对象的数据集?