如何为屏幕截图并分享到社交媒体或电子邮件

时间:2019-03-04 23:06:56

标签: c# xamarin share screenshot screen-capture

:) 我正在尝试实现屏幕快照服务,该服务负责制作页面屏幕快照并在用户已安装或emial的社交媒体平台上共享。作为标准共享工具栏。我只能共享来自ur的图像,如下是我卡住​​的代码:

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

我为当前活动实现了截图服务,这是我要求分享的内容。

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

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

1 个答案:

答案 0 :(得分:0)

请看一下经过测试的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;
 }