【问题标题】:How to caputre screen and share it to social media or email如何捕获屏幕并将其分享到社交媒体或电子邮件
【发布时间】:2019-07-26 08:11:33
【问题描述】:

:) 我正在尝试实现截图服务,该服务负责制作页面截图并在用户安装或 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;
    }
}

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

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

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

【问题讨论】:

  • 获取字节图像数据时,似乎没有转换使用的数据类型。根据你分享图片的方式,地址就是地址,所以你拿屏获取字节数据的时候,必须先保存到本地才能获取地址。stackoverflow.com/questions/46103368/…

标签: c# xamarin share screenshot screen-capture


【解决方案1】:

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

【讨论】:

  • 我试过了,但我找不到一种方法来附加这个文件来分享它:(我应该使用一些转换器从保存的字符串路径到字节吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
相关资源
最近更新 更多