0

I using mapsui to show an IGN map on my application and I want to make a screenshot of the map when I click on a specific button. So I use dependency injection and It works perfectly on UWP. But with Android, I can't screen the map and I have a white screen. There is my code for Android :

public Task<byte[]> TakeShot()
    {
        var _activity = CrossCurrentActivity.Current;
        if (_activity == null)
        {
            throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
        }

        var view = _activity.Activity.Window.DecorView;
        view.DrawingCacheEnabled = true;

        Bitmap bitmap = view.GetDrawingCache(true);
        view.BuildDrawingCache();

        byte[] bitmapData;

        using (var stream = new MemoryStream())
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        return Task.FromResult(bitmapData);
    }

Only the map is blank, because I have my button on the image. Thank's for your help

Alexandre S.
  • 522
  • 2
  • 5
  • 24

1 Answers1

0

I used the Mapsui, then I create a screenshot function with dependence service, Here is my GIF when take a screemshot.

enter image description here

Here is my dependence service about achieveing screenshot.

[assembly: Dependency(typeof(ScreenshotService))]
namespace MVVMDataBinding.Droid
{
 public class ScreenshotService : IScreenshotService
 {

    public byte[] Capture()
    {

       var _activity = CrossCurrentActivity.Current;

        var rootView = _activity.Activity.Window.DecorView;

        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();
            }
        }
    }
   }
}

And create an interface IScreenshotService in the xamarin forms.

   public interface IScreenshotService
{
    byte[] Capture();
}

Use it in the xamarin forms.

private void Button_Clicked(object sender, EventArgs e)
    {
        var screenshotData = DependencyService.Get<IScreenshotService>().Capture();
        myImage.Source=ImageSource.FromStream(() => new 
        MemoryStream((byte[])screenshotData));
    }

forground code.

  <ContentPage.Content>
    <StackLayout>
    <Grid x:Name="ContentGrid"  WidthRequest="100" HeightRequest="200" />

    <Button Text="Take a screen" Clicked="Button_Clicked"/>

    <Image x:Name="myImage" WidthRequest="200" HeightRequest="300"/>
    </StackLayout>
</ContentPage.Content>

Here is my demo.

https://github.com/851265601/MapsuiWithScreenshotDemo

Leon
  • 8,404
  • 2
  • 9
  • 52
  • Thank's for your answer, but I still got a when screenshot, even with your code. Do you know if some autorizations are needed ? Or if it's because of the provider ? – Alexandre S. Nov 19 '19 at 09:04
  • @AlexandreS. Nope, did you download my demo to make a test in your device? If my demo cannot running normally in your device, I suggest you to change another device to make a test.I running in my code in Pixel, it can running normally. – Leon Nov 19 '19 at 09:46
  • I download your demo and try on an other device, but i can't lauch your project – Alexandre S. Nov 19 '19 at 15:51
  • Can you share the error when you run my project? Or you can share a demo, I will help you to toubleshot it. – Leon Nov 20 '19 at 07:40
  • `Failed to create JavaTypeInfo for class: Android.Support.V4.View.Accessibility.AccessibilityManagerCompat/IAccessibilityStateChangeListenerImplementor due to System.IO.DirectoryNotFoundException: Impossible de trouver une partie du chemin d'accès 'C:\Users\alexandre\Desktop\MapsuiWithScreenshotDemo-master\MapsuiWithScreenshotDemo-master\MVVMDataBinding\MVVMDataBinding.Android\obj\Debug\90\android\src\mono\android\support\v4\view\accessibility\AccessibilityManagerCompat_AccessibilityStateChangeListenerImplementor.java'.` – Alexandre S. Nov 20 '19 at 08:00
  • Your path length is too long, please shortened your path, here is a similar case https://developercommunity.visualstudio.com/content/problem/521034/failed-to-create-javatypeinfo.html# – Leon Nov 20 '19 at 08:36
  • Thank you, it work great ! But even with your code, I still got a blank screen ... – Alexandre S. Nov 20 '19 at 08:40
  • please create a emulator like my settings,https://imgur.com/a/uSq1Oxq then run my demo. I test my demo in HUAWei device , Pixel and above emulaor, all of them could take a screenshot. – Leon Nov 20 '19 at 09:03
  • Oh sorry, I want to say, even if I took your code in my project, I still got a blank on my project. Your demo runs fine. – Alexandre S. Nov 20 '19 at 09:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202711/discussion-between-alexandre-s-and-leon-lu-msft). – Alexandre S. Nov 20 '19 at 10:57
  • I am coming back, if my demo could be running normally, This issue is related to your project, can you provide a demo to me? To protect your privacy, please do not provide your project. – Leon Nov 21 '19 at 05:14