Jump to content
softtouch

Screenshot on macOS

Recommended Posts

I am trying to figure out hot to make a screenshot via code of the own app when running on macOS, but could not find a solution.

Does anybody here has a clue for me? Windows is no problem, just macOS cause headache.

Edited by softtouch

Share this post


Link to post

I use this, you should be able to adapt for your use.  RootComponent is a TForm.

Reference:

https://www.fmxexpress.com/create-device-scaled-screenshots-in-delphi-xe5-firemonkey-on-android-and-ios/

function TSERTTKRepoForm.ScreenShot: TTMSFNCBitmap;
var
  lms: TMemoryStream;
  bmp: TBitmap;
  fScreenScale: Single;
  Surf: TBitmapSurface;
  function GetScreenScale: Single;
  var
    ScreenService: IFMXScreenService;
  begin
    result := 1;
    if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) then
    begin
      result := ScreenService.GetScreenScale;
    end;
  end;
 begin
  lms := TMemoryStream.Create;
  try
    lms.position := 0;
    fScreenScale := GetScreenScale;
    bmp := TBitmap.Create(Round(TForm(RootComponent).Canvas.Width * fScreenScale),
      Round(TForm(RootComponent).Canvas.Height * fScreenScale));
    bmp.Clear(0);
    if bmp.Canvas.BeginScene then
      try
        TForm(RootComponent).PaintTo(bmp.Canvas);
      finally
        bmp.Canvas.EndScene;
      end;
    Surf := TBitmapSurface.Create;
    Surf.Assign(bmp);
    if not TBitmapCodecManager.SaveToStream(lms, Surf, '.png') then
      raise EBitmapSavingFailed.Create('Error saving Bitmap to png');
    Surf.Free;
    bmp.Free;
    lms.position := 0;
    result := TTMSFNCBitmap.CreateFromStream(lms);
  finally
    lms.Free;
  end;
end;

 

  • Like 1
  • Thanks 1

Share this post


Link to post

I will play with it, thanks.

 

Edit:

Tested and works just fine 🙂

Edited by softtouch

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×