0

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

Snapper
  • 3
  • 3

1 Answers1

0

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();
        }
    }
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Please see the edit, Thank you – Snapper Dec 17 '21 at 07:45
  • At first, you can use the Android native command to do that such as 'Java.Lang.Runtime.GetRuntime().Exec("screencap -p /sdcard/screen.png");' In addition, you can use the MediaProjection class to take the screenshot.It is recommended to read the offical document about them before using them. And there is a example for the MediaProjection class.[link](https://github.com/xamarin/monodroid-samples/blob/master/android5.0/ScreenCapture/ScreenCapture/ScreenCaptureFragment.cs) @Snapper – Liyun Zhang - MSFT Dec 20 '21 at 06:42