Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey, Delphi Android, Delphi IOS

Create Device Scaled Screenshots In Delphi XE5 Firemonkey On Android And IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE5 Scaled MakeScreenshot | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSIf you’ve ever used the function MakeScreenshot which is part of most Firemonkey controls on mobile you may have noticed that it does not take a screenshot that is scaled to the device scale. So if you use MakeScreenshot from a TLayout the bitmap you end up with is most likely not the same size as the TLayout on the screen when used on mobile devices like Android and IOS. One solution to this is to get the screen scale using the GetScreenScale found in the TPlatformServices object. I have put together a function that makes this easier to do.
Usage:

var
B: TBitmap;
begin
B := MakeScaleScreenshot(Self);
Image2.Bitmap.Assign(B);
B.DisposeOf;
end;

Function:

function MakeScaleScreenshot(Sender:TControl): TBitmap;
var
fScreenScale: Single;
function GetScreenScale: Single;
var
ScreenService: IFMXScreenService;
begin
Result := 1;
if TPlatformServices.Current.SupportsPlatformService (IFMXScreenService, IInterface(ScreenService)) then
begin
Result := ScreenService.GetScreenScale;
end;
end;
begin
fScreenScale := GetScreenScale;
Result := TBitmap.Create(Round(Sender.Width*fScreenScale), Round(Sender.Height*fScreenScale));
Result.Clear(0);
if Result.Canvas.BeginScene then
try
Sender.PaintTo(Result.Canvas, RectF(0,0,Result.Width,Result.Height));
finally
Result.Canvas.EndScene;
end;
end;
There are a couple StackOverflow questions relevant to to this function and are what I used when researching this function. There is also a topic at Embarcadero about multiresolution bitmaps. They are:

FireMonkey TControl.MakeScreenshot generates an undersized bitmap on Mobile platforms

Firemonkey drawing on bitmap without interpolation

Using Multi-Resolution Bitmaps In Delphi XE5 Firemonkey On Android And IOS

Exit mobile version