问题描述
是否有一个可以在xamarin.forms中进行屏幕捕获的软件包?
Is there a package that does screen capture in xamarin.forms ?
我还需要捕获Google地图的屏幕截图
I need also to capture google maps screen shots
推荐答案
查看博客文章.
Check out this blog post by Daniel Hindrikes.
我将假设您使用PCL作为共享代码.
I'm going to assume that you use a PCL for your shared code.
您将需要在PCL中创建一个接口.他称其为IScreenshotManager
.声明看起来像这样:
You will need to create an interface in your PCL. He calls it IScreenshotManager
. The declaration looks like this:
public interface IScreenshotManager
{
Task<byte[]> CaptureAsync();
}
现在所有平台都将有自己的实现.对于iOS;
Now all platforms will have their own implementation for it.For iOS;
public class ScreenshotManager : IScreenshotManager
{
public async System.Threading.Tasks.Task<byte[]> CaptureAsync()
{
var view = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
UIGraphics.BeginImageContext(view.Frame.Size);
view.DrawViewHierarchy(view.Frame, true);
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
using(var imageData = image.AsPNG())
{
var bytes = new byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, bytes, 0, Convert.ToInt32(imageData.Length));
return bytes;
}
}
}
对于Android:
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;
}
}
对于Windows Phone:
And for Windows Phone:
public class ScreenshotManager : IScreenshotManager
{
public async Task<byte[]> CaptureAsync()
{
var rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
var screenImage = new WriteableBitmap((int)rootFrame.ActualWidth, (int)rootFrame.ActualHeight);
screenImage.Render(rootFrame, new MatrixTransform());
screenImage.Invalidate();
using (var stream = new MemoryStream())
{
screenImage.SaveJpeg(stream, screenImage.PixelWidth, screenImage.PixelHeight, 0, 100);
var bytes = stream.ToArray();
return bytes;
}
}
}
不要忘记使用依赖性服务,如下所示:
Don't forget to register your platform specific implementations with the attribute which registers it with the Dependency Service, like this:
[assembly: Xamarin.Forms.Dependency (typeof (ScreenshotManager))]
它超出了名称空间声明.
It goes above the namespace declaration.
现在,从您的共享代码中,您可以通过如下调用获得屏幕截图的byte[]
:
Now from your shared code you would be able to get the byte[]
of a screenshot with a call like this:
var screenshotBytes = DependencyService.Get<IScreenshotManager>().CaptureAsync();
您可能想在使用DependencyService.Get<IScreenshotManager>()
之前不是null
.
You probably want to check if DependencyService.Get<IScreenshotManager>()
isn't null
before using it.
之后,您可以将byte[]
转换为图像,并随便使用它!
After that you can turn your byte[]
into an image and do whatever you like with it!
这篇关于在xamarin.forms中进行屏幕捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!