How to keep an image on a form?

1

Hello! To use in game production, I'd like to understand how to keep an image in a form through Firemonkey. The code I have so far is this:

program TestCase;

uses
    UITypes,   Classes,      Types,
    FMX.Forms, FMX.Graphics, FMX.Objects;

type
    TMainForm = class(TForm)
        constructor CreateNew(AOwner: TComponent; Dummy: NativeInt = 0); Override;
        procedure AppEnd(Sender : TObject; var Action : TCloseAction);
        procedure PaintStuff(Sender : TObject; Canvas : TCanvas);
    end;

var
    T : TThread;
    B : TBitmap;
    F : TMainForm; 

{ TMainForm }


procedure TMainForm.AppEnd(Sender: TObject; var Action: TCloseAction);
begin
    T.Terminate;
    Action := TCloseAction.caFree;
    Application.Terminate;
end;

constructor TMainForm.CreateNew(AOwner: TComponent; Dummy: NativeInt = 0);
begin
    inherited CreateNew(nil);
    with TPaintBox.Create(Self) do
    begin
        Width   := ClientWidth;
        Height  := ClientHeight;
        Parent  := Self;
        OnPaint := PaintStuff;
    end;
    OnClose := AppEnd;
end;

procedure TMainForm.PaintStuff(Sender: TObject; Canvas: TCanvas);
begin
    Canvas.BeginScene();
    Canvas.DrawBitmap(B, ClientRect, ClientRect, 100);
    Canvas.EndScene;
end;    

begin
    Application.Initialize;
    B := TBitmap.CreateFromFile('test.png');
    B.SetSize(90, 100);
    F := TMainForm.CreateNew(nil);
    F.Show;
    T := TThread.CreateAnonymousThread(procedure() begin
        F.Invalidate;
        TThread.Sleep(10);
    end);
    T.Start;
    Application.Run;
end.

The above excerpt throws no exception, and according to the debugger , all code is running. However, the image is not displayed.

Am I forgetting something? Do I need any more configuration I'm not aware of? Am I doing everything wrong? Any suggestions?

    
asked by anonymous 16.01.2016 / 16:59

1 answer

0

As stated in the comments, for whatever reason the SetSize method deletes the current contents of bitmap . Therefore, removing the call to this method causes the image to remain in the form.

If you need to set the image size, you should follow this order:

  • Create object TBitmap .
  • Set the size with SetSize .
  • And just finally, load an image with LoadFromFile .
  • 01.07.2016 / 18:13