/// <summary>
/// IScreenshotService
/// </summary>
public interface IScreenshotService
{
byte[] Capture();
}
/// <summary>
/// ScreenshotService
/// </summary>
public class ScreenshotService ///: IScreenshotService
{
private static Activity _currentActivity;
public static void SetActivity(Activity activity)
{
_currentActivity = activity;
}
public static byte[] Capture()
{
var rootView = _currentActivity.Window.DecorView.RootView;
using (var screenshot = Bitmap.CreateBitmap(
rootView.Width,
rootView.Height,
Bitmap.Config.Argb8888))
{
var canvas = new Canvas(screenshot);
rootView.Draw(canvas);
using (var stream = new MemoryStream())
{
screenshot.Compress(Bitmap.CompressFormat.Png, 90, stream);
return stream.ToArray();
}
}
}
}
Его вызов:
int screens_count = 5; // ЧИСЛО СКРИНОВ
byte[] screenshotData = null;
string imagePath = "";
for (int i = 0; i < screens_count; i++)
{
//byte[] screenshotData = DependencyService.Get<XamarinFL34.Droid.IScreenshotService>().Capture();
screenshotData = XamarinFL34.Droid.ScreenshotService.Capture();
// Save ScreenShot Data to File //
//var imagePath = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString(), "XamarinFL34");
imagePath = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).Path, "XamarinFL34");
if (!System.IO.Directory.Exists(imagePath.ToString()))
{
Directory.CreateDirectory(imagePath);
}
imagePath = System.IO.Path.Combine(imagePath, "img" + System.Environment.TickCount + ".jpg");
using (var fileOutputStream = new FileOutputStream(imagePath))
{
await fileOutputStream.WriteAsync(screenshotData);
}
// Sleep current thread
await Task.Delay(2000); //Simulates a 0.5 second wait
}
// Show Message
await DisplayAlert("Сообщение", screens_count + " ScreenShots saved, last file: " + imagePath, "OK");