Esempio n. 1
0
        private async Task TakeScreenshot()
        {
            RunOnUiThread(() =>
            {
                _commentView.Visibility = ViewStates.Visible;

                _gridView.Visibility = ViewStates.Invisible;
                _drawView.Visibility = ViewStates.Invisible;

                _pilotVienna.Visibility = ViewStates.Invisible;
                _pilotTim.Visibility    = ViewStates.Invisible;
                _pilotMario.Visibility  = ViewStates.Invisible;
                _pilotTwan.Visibility   = ViewStates.Invisible;
                _pilotJudith.Visibility = ViewStates.Invisible;
                _pilotSanne.Visibility  = ViewStates.Invisible;
            });

            await Task.Delay(50).ConfigureAwait(false);

            _root.DrawingCacheEnabled = true;
            _root.BuildDrawingCache(true);
            var bitmap = Bitmap.CreateBitmap(_root.DrawingCache);

            _root.DrawingCacheEnabled = false;

            var fileName = string.Format("{0}.png", DateTimeOffset.Now.ToString());

            var basePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            var filePath = System.IO.Path.Combine(basePath, "United Wings", fileName);

            if (!Directory.Exists(System.IO.Path.Combine(basePath, "United Wings")))
            {
                Directory.CreateDirectory(System.IO.Path.Combine(basePath, "United Wings"));
            }

            var stream = new FileStream(filePath, FileMode.Create);
            await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, stream).ConfigureAwait(false);

            stream.Close();

            RunOnUiThread(() =>
            {
                _commentView.Visibility = ViewStates.Invisible;

                _gridView.Visibility = ViewStates.Visible;
                _drawView.Visibility = ViewStates.Visible;

                _pilotVienna.Visibility = ViewStates.Visible;
                _pilotTim.Visibility    = ViewStates.Visible;
                _pilotMario.Visibility  = ViewStates.Visible;
                _pilotTwan.Visibility   = ViewStates.Visible;
                _pilotJudith.Visibility = ViewStates.Visible;
                _pilotSanne.Visibility  = ViewStates.Visible;

                Toast.MakeText(BaseContext, "Screenshot saved", ToastLength.Short).Show();
            });

            MediaScannerConnection.ScanFile(ApplicationContext, new string[] { filePath }, null, this);
        }