I haven't find a function to get a screenshot in FMX.Platform (anyway, nowhere else...).
With the VCL, there are many answers (stackoverflow, google, ...).
But how to get a screenshot in an image(bitmap or whatever) for Windows and Mac OS X?
Regards,
W.
Update: The link from Tipiweb gives a good solution for OS X.
Regarding the Windows part: I have coded this, but I don't like to use the VCL, and a Stream to achieve it... Any better suggestion, comments?
Thanks.
W.
uses ..., FMX.Types, Winapi.Windows, Vcl.Graphics;
...
function DesktopLeft: Integer;
begin
Result := GetSystemMetrics(SM_XVIRTUALSCREEN);
end;
function DesktopWidth: Integer;
begin
Result := GetSystemMetrics(SM_CXVIRTUALSCREEN);
end;
function DesktopTop: Integer;
begin
Result := GetSystemMetrics(SM_YVIRTUALSCREEN);
end;
function DesktopHeight: Integer;
begin
Result := GetSystemMetrics(SM_CYVIRTUALSCREEN);
end;
procedure GetScreenShot(var dest: FMX.Types.TBitmap);
var
cVCL : Vcl.Graphics.TCanvas;
bmpVCL: Vcl.Graphics.TBitmap;
msBmp : TMemoryStream;
begin
bmpVCL := Vcl.Graphics.TBitmap.Create;
cVCL := Vcl.Graphics.TCanvas.Create;
cVCL.Handle := GetWindowDC(GetDesktopWindow);
try
bmpVCL.Width := DesktopWidth;
bmpVCL.Height := DesktopHeight;
bmpVCL.Canvas.CopyRect(Rect(0, 0, DesktopWidth, DesktopHeight),
cVCL,
Rect(DesktopLeft, DesktopTop, DesktopLeft + DesktopWidth, DesktopTop + DesktopHeight)
);
finally
ReleaseDC(0, cVCL.Handle);
cVCL.Free;
end;
msBmp := TMemoryStream.Create;
try
bmpVCL.SaveToStream(msBmp);
msBmp.Position := 0;
dest.LoadFromStream(msBmp);
finally
msBmp.Free;
end;
I build a small application to take screenshot (Windows / Mac) and it works :-) !
For windows and Mac compatibility, I use a stream.
After that, I load my Windows or Mac TStream in a FMX.Types.TBitmap (with load from stream)
Windows Unit code :
Mac Unit Code :
In your mainForm unit :
If you have another idea, please talk to me :-)
Thanks to Tipiweb's code (in his answer), a github project has been started based on it; with some improvements (ability to take a screenshot only of a certain window, or take a full screenshot).
The unit is named xscreenshot.pas (single unit for all platforms)
The github project page:
The utilities available in this unit:
Finishing touches on MacOS need some work for taking a screenshot of a specific window.
Again, thanks to Tipiweb and his answer to get this project started.
You can use a good solution from this site to do a Mac OSX screenshot.
Do the same works with the Windows API like this:
After that, include your different units with: