[delphi]向ImageList中加入png類型的資源圖片


向ImageList中動態加入Png圖片有些失真,經過多方查詢,發現需要將Bitmap的AlphaFormat指定一下。

下面代碼支持多層Png導入到ImageList。

//向ImageList中加入png類型的資源圖片
procedure AddPngToImageList(AImageList: TImagelist; AResName: string);
var
  LResStream: TStream;
  LPng: TPngImage;
  LBitMap, LRowBitMap: TBitmap;
  LRect, LRowRect: TRect;
begin
  //AImageList.DrawingStyle := dsTransparent;
  AImageList.ColorDepth := cd32Bit;
  LResStream := TResourceStream.Create(uResourcesImpl.GetResHandle, AResName, RT_RCDATA);
  LPng := TPngImage.Create;
  LBitMap := TBitmap.Create;
  try
    LPng.LoadFromStream(LResStream);
    LBitMap.Assign(LPng);
    LBitMap.AlphaFormat := afIgnored;
    if LBitMap.Height > AImageList.Height then
    begin
      LRowBitMap := TBitmap.Create;
      try
        LRowBitMap.Width := LBitMap.Width;
        LRowBitMap.Height := AImageList.Height;
        LRect := Rect(0, 0, LBitMap.Width, LRowBitMap.Height);
        LRowRect := Rect(0, 0, LRowBitMap.Width, LRowBitMap.Height);
        while LRect.Bottom <= LBitMap.Height do
        begin
          LRowBitMap.Canvas.CopyRect(LRowRect, LBitMap.Canvas, LRect);
          OffsetRect(LRect, 0, LRowBitMap.Height);
          AImageList.Add(LRowBitMap, nil);
        end;
      finally
        LRowBitMap.Free;
      end;
    end
    else
      AImageList.Add(LBitMap, nil);
  finally
    LResStream.Free;
    LPng.Free;
    LBitMap.Free;
  end;
end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM