在 xamarin.forms 中制作屏幕截图 [英] Making screen capture in xamarin.forms

查看:30
本文介绍了在 xamarin.forms 中制作屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有在 xamarin.forms 中进行屏幕捕获的包?

Is there a package that does screen capture in xamarin.forms ?

我还需要捕捉谷歌地图屏幕截图

I need also to capture google maps screen shots

推荐答案

查看 这篇 博文,由 Daniel Hindrikes 撰写.

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

    }
}

安卓:

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().CaptureAsync();

您可能想在使用之前检查 DependencyService.Get() 是否不是 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 中制作屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆