How to keep an image in a form?

Asked

Viewed 66 times

1

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

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 section makes no exception and according to the Debugger, all the code is running. However the image is not shown.

Am I forgetting something? Do I need any other settings I’m not aware of? Am I doing everything wrong? Any suggestions?

  • Have you tried drawing without bitmap? Why are you using a thread in this example?

  • Otherwise I’d need one TTimer, but as I will need other processing I preferred a thread. However I have already discovered the problem. I am using the SetSize right after the creation of TBitmap and image loading. And this apparently erases the current content of TBitmap. Without the SetSize the code above works.

  • 1

    in this case, you should answer that question yourself.

  • @Guill, you would have to change your code and add as a response? :)

1 answer

0


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

In case of need to define the size of the image, it should follow this order:

  1. Create the object TBitmap.
  2. Set the size with SetSize.
  3. And just finally, load an image with LoadFromFile.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.