繁体   English   中英

如何截取用户可以看到的内容 xamarin forms

[英]How to take a screenshot of what the user can see xamarin forms

我看过许多其他使用 Xamarin.Essentials.Screenshot.CaptureAsync() 的帖子。 问题在于它截取了最顶层视图的屏幕截图,而不是用户可以看到的屏幕截图。

我的问题是,是否可以截取用户可以看到的内容,如果可以,如何截取?

下面的代码可以得到一个bitmap的活动视图,然后你可以把bitmap作为图像文件保存到设备中。

    private void takeScreenShot(Activity activity)
    {
        View view = activity.Window.DecorView;
        view.DrawingCacheEnabled = true;
        view.BuildDrawingCache();
        Bitmap bitmap = view.DrawingCache;
        Rect rect = new Rect();
        activity.Window.DecorView.GetWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.Top;
        int width = activity.WindowManager.DefaultDisplay.Width;
        int height = activity.WindowManager.DefaultDisplay.Height;
        Bitmap screenShotBitmap = Bitmap.CreateBitmap(bitmap, 0, statusBarHeight, width,
         height - statusBarHeight);
        view.DestroyDrawingCache();
        string uri = System.IO.Path.Combine("/sdcard/Download", "screen.jpg");
        using (System.IO.FileStream os = new System.IO.FileStream(uri, System.IO.FileMode.Create))
        {
            screenShotBitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            os.Close();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM