-1

I want to take google map screenshot in my xamarin PCL Project. The map is displaying perfectly. But when i try to take screenshot it is displaying black screen of the google map part.

Map is displaying on screen like below

But when i try to take by code screenshot is displaying like below. Screenshot via code

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


public class ScreenshotService : IScreenshotService
{
    public static Activity Activity { get; set; }

    //public void SetActivity(Activity activity) => _currentActivity = activity 

    public async System.Threading.Tasks.Task<byte[]> Capture()
    {
        if (Activity == null)
        {
            throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
        }

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

        Bitmap bitmap = view.GetDrawingCache(true);

        byte[] bitmapData;

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

        return bitmapData;
    }
}

Code to for calling function

byte[] screenshotData = await DependencyService.Get<IScreenshotService>().Capture();

can you help me to figure out the issue?

A.Goutam
  • 3,422
  • 9
  • 42
  • 90
  • Try : Call this `view.BuildDrawingCache();` after `GetDrawingCache` and then put some delay before capturing screen. – Gaurang Dave Feb 25 '19 at 06:17
  • @GaurangDave is this will be find Bitmap bitmap = view.GetDrawingCache(true); view.BuildDrawingCache(); await Task.Delay(1000); – A.Goutam Feb 25 '19 at 06:24
  • `view.DrawingCacheEnabled = true;view.BuildDrawingCache(); await Task.Delay(1000); Bitmap bitmap = view.GetDrawingCache(true);` I suggest this but I would request you to try these statement in different sequence as I can not code it right now. – Gaurang Dave Feb 25 '19 at 06:28

2 Answers2

2

I think it might be Images of the map must not be transmitted to your servers, or otherwise used outside of the application and you could use GoogleMap.snapshot method to take snapshots of the map

simply implement the following interface: onSnapshotReady

public abstract void onSnapshotReady (Bitmap snapshot)

and call:snapshot

public final void snapshot (GoogleMap.SnapshotReadyCallback callback)

you alse could refer to the example (in jave) :screenshot answer

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • thanks for your comments but i want to do this in Xamarin Forms Application – A.Goutam Feb 26 '19 at 05:53
  • 1
    i know .i mean you use DependencyService to call the ScreenshotService ,then you could write the code in Droid (only conversion from java to C# ,refer to the document and link) – Leo Zhu Feb 26 '19 at 05:59
0

This might be because what you are doing is against the Terms of Service of Google Maps Platform, according to the article:

3.2.4a "No Scraping. Customer will not extract, export, scrape, or cache Google Maps Content for use outside the Services."

You may visit Google Maps Platform Terms of Service to learn more about Licensing.

Shawn Domingo
  • 1,371
  • 1
  • 11
  • 16
  • thanks for your reply but i can take screen shot from different Screen shot app for the same screen – A.Goutam Feb 27 '19 at 07:43