我是Xamarin.Forms的初学者,我有一个问题:如何在我的共享代码/ xaml.cs中调用以下代码?
public class screenshotManager : IScreenshotManager
{
public static Activity Activity { get; set; }
public async System.Threading.Tasks.Task<byte[]> CaptureAsync()
{
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;
}
}
答案 0 :(得分:0)
如果您需要从字节[]中获取图像,正如我们在评论中所讨论的那样,那么您应该像@Jason所说的那样。
这样的事情:
var imageBytes = await screenshotManager.CaptureAsync();
Image imageView = new Image();
imageView.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));