I've seen many other posts that use Xamarin.Essentials.Screenshot.CaptureAsync(). The problem with that is that it takes the screenshot of the top-most view and not what the user can see.
My question is, is it is possible to take a screenshot of what the user can see and if it is, how?
EDIT :
I haven't been very clear of what I am trying to achieve, so here is it:
I am trying to take a screenshot of the phone default screen(where all the apps are etc.) and also other apps. So for that I use a foreground service. It works and all but the screenshots are still only the view of the app
CodePudding user response:
The following code can get the view of the activity with a bitmap, and then you can save the bitmap as a image file to the device.
private void takeScreenShot(Activity activity)
{
View view = activity.Window.DecorView;
view.DrawingCacheEnabled = true;
view.BuildDrawingCache();
Bitmap bitmap = view.DrawingCache;
Rect rect = new Rect();
activity.Window.DecorView.GetWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.Top;
int width = activity.WindowManager.DefaultDisplay.Width;
int height = activity.WindowManager.DefaultDisplay.Height;
Bitmap screenShotBitmap = Bitmap.CreateBitmap(bitmap, 0, statusBarHeight, width,
height - statusBarHeight);
view.DestroyDrawingCache();
string uri = System.IO.Path.Combine("/sdcard/Download", "screen.jpg");
using (System.IO.FileStream os = new System.IO.FileStream(uri, System.IO.FileMode.Create))
{
screenShotBitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
os.Close();
}
}