如何捕获屏幕并将其分享到社交媒体或电子邮件

How to caputre screen and share it to social media or email

:) 我正在尝试实现截图服务,该服务负责制作页面截图并在用户安装或 emial 的社交媒体平台上分享。作为标准共享工具栏。我只能分享你的图片,下面是我卡住的代码:

public class ShareImage : Activity, IShareImage
{
    public Task Share(string screenshotPath)
    {
        var path = Android.OS.Environment.GetExternalStoragePublicDirectory("Temp");

        if (!File.Exists(path.Path))
        {
            Directory.CreateDirectory(path.Path);
        }

        string absPath = path.Path + "tempfile.jpg";
        File.WriteAllBytes(absPath, GetBytes(screenshotPath));

        var _context = Android.App.Application.Context;

        Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);

        sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Application Name");

        sendIntent.SetType("image/*");

        sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + absPath));
        _context.StartActivity(Intent.CreateChooser(sendIntent, "Sharing"));
        return Task.FromResult(0);
    }

    public static byte[] GetBytes(string screenshotPath)
    {
        //var stream = new MemoryStream(await CrossScreenshot.Current.CaptureAsync());
        byte[] bytes;
        bytes = DependencyService.Get<IScreenshotService>().CaptureScreenAsync();
        return bytes;
    }
}

我为当前 activity 实施了屏幕截图服务,这是我要求分享的内容。

public class ScreenshotService : IScreenshotService
{
    private Activity _currentActivity;

    public void SetActivity(Activity activity)
    {
        _currentActivity = activity;
    }
    public byte[] Caputre()
    {
        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();
            }
        }
    }
}

对于这两项服务,我在实现它时遇到了问题,因为它共享的不是 url 字符串,而是我当前的页面外观。应用程序抛出异常

Object reference not set to an instance of an object.

我的视图模型

 public class MainViewModel : INotifyPropertyChanged
{
    private readonly INavigation navigation;
    public MainViewModel()
    {
        //TakeScreenshotCommand = new Command(CaptureScreenshot);
        //sample data 
        SampleImageUrl = "minusikona.png";
        ShareImageCommand = new Command(() => SharingImage());
    }

    private async void SharingImage()
    {
        try
        {
            await DependencyService.Get<IShareImage>().Share(ScreenshotImage);
        }
        catch(Exception ex)
        {
            Console.Write(ex);
            await App.Current.MainPage.DisplayAlert("Eror", "Something went wrong! ;(. Please try again later! :) ", "OK");
        }
    }

    public string SampleImageUrl { get; set; }

    public ICommand TakeScreenshotCommand { get; set; }
    public ICommand ShareImageCommand { get; set; }
    private string _screenshotImage;
    public string ScreenshotImage
    {
        get => _screenshotImage;
        set
        {
            if (_screenshotImage != value)
            {
                _screenshotImage = value;
                OnPropertyChanged("_screenshotImage");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

我想不通。对于如何进一步推动它的任何帮助,我将非常有帮助。

请查看已经过测试的 3d 派对解决方案,例如 https://github.com/wilsonvargas/ScreenshotPlugin

更新:

public byte[] LoadImage(string filename)
{ 
    var image UIImage.FromFile(filename);

    using (NSData imageData = image.AsPNG()) {
      byte[] myByteArray = new Byte[imageData.Length];
   System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes,myByteArray, 0, Convert.ToInt32(imageData.Length));
    return myByteArray;
 }