John Kouraklis
John Kouraklis

Reputation: 686

How to assign a bitmap to TSpeedButton in Delphi Firemonkey

I am trying to assign a bitmap to a speedbutton in FMX Delphi. In design time, I create a TImageList, load my image and then assign one to the speedbutton.

How do I do it programmatically?

Upvotes: 0

Views: 3001

Answers (2)

In the TSpeedButton you should set Images and ImageIndex. To load pictures into TImageList you can use AddOrSet or you can use this example

procedure TForm11.Button2Click(Sender: TObject);
const
  SourceName = 'Картинка';
  procedure LoadPicture(const Source: TCustomSourceItem; const Scale: Single; const FileName: string);
  var
    BitmapItem: TCustomBitmapItem;
    TmpBitmap: TBitmap;
  begin
    BitmapItem := Source.MultiResBitmap.ItemByScale(Scale, True, True);
    if BitmapItem = nil then
    begin
      BitmapItem := Source.MultiResBitmap.Add;
      BitmapItem.Scale := Scale;
    end;
    BitmapItem.FileName := FileName;
    TmpBitmap := BitmapItem.CreateBitmap;
    try
      if TmpBitmap <> nil then
        BitmapItem.Bitmap.Assign(TmpBitmap);
    finally
      TmpBitmap.Free;
    end;
  end;
var
  NewSource: TCustomSourceItem;
  NewDestination: TCustomDestinationItem;
  NewLayer: TLayer;
begin
  if ImageList1.Source.IndexOf(SourceName) = -1 then
  begin
    NewSource := ImageList1.Source.Add;
    NewSource.Name := SourceName;
    NewSource.MultiResBitmap.TransparentColor := TColorRec.Fuchsia;
    NewSource.MultiResBitmap.SizeKind := TSizeKind.Custom;
    NewSource.MultiResBitmap.Width := 16;
    NewSource.MultiResBitmap.Height := 16;
    LoadPicture(NewSource, 1, 'D:\Мои веселые картинки\Icons\16x16\alarm16.bmp');
    LoadPicture(NewSource, 1.5, 'D:\Мои веселые картинки\Icons\24x24\alarm24.bmp');
    NewDestination := ImageList1.Destination.Add;
    NewLayer := NewDestination.Layers.Add;
    NewLayer.SourceRect.Rect := TRectF.Create(TPoint.Zero, NewSource.MultiResBitmap.Width,
      NewSource.MultiResBitmap.Height);
    NewLayer.Name := SourceName;
    ControlAction1.ImageIndex := NewDestination.Index;
  end;
end;

Upvotes: 1

Freddie Bell
Freddie Bell

Reputation: 2287

var
  Size: TSizeF;
begin
  Size := TSize.Create(64,64)
  Bitmap1.Assign(Imagelist1.Bitmap(Size, Index));
end

Upvotes: 2

Related Questions