Xamarin.Forms - Capture ScreenShot Using DependencyService

 Introduction

Xamarin 
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.

DependencyService

DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do.

DependencyService is a dependency resolver. In practice, an interface is defined and DependencyService finds the correct implementation of that interface from the various platform projects.
Xamarin
 
Xamarin.Forms apps need three components to use DependencyService
  1. Interface
    The required functionality is defined by an interface in shared code.

  2. Implementation Per Platform
    Classes that implement the interface must be added to each platform project.

  3. Registration
    Each implementing class must be registered with DependencyService via a metadata attribute. Registration enables DependencyService to find the implementing class and supply it in place of the interface at runtime.

  4. Call to DependencyService
    Shared code needs to explicitly call DependencyService to ask for implementations of the interface.
Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.

Xamarin

Now Select the Blank App and Choose Portable Class Library(PCL).

Xamarin

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Xamarin
 
Setting up the User Interface

Go to MainPage.Xaml and write the following code.
 
Xamarin
MainPage.Xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinFormsScreenShot" x:Class="XamarinFormsScreenShot.MainPage">  
  3.     <ContentPage.Content>  
  4.         <StackLayout>  
  5.             <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">  
  6.                 <Image x:Name="imgBanner" WidthRequest="300"></Image>  
  7.                 <Button Text="Capture Me" WidthRequest="100" x:Name="btnCapture" Clicked="btnCapture_Clicked"></Button>  
  8.                 <Frame BackgroundColor="Gray" Margin="10" Padding="5">  
  9.                     <Image x:Name="imgCaptured"></Image>  
  10.                 </Frame>  
  11.             </StackLayout>  
  12.         </StackLayout>  
  13.     </ContentPage.Content>  
  14. </ContentPage>  
Click the Play button to try it out.

Xamarin 
 
Creating Interface

Create - interface in Xamarin.Forms PCL.

Go to Solution—>PCL—>Right click—>New—>Interface—>IScreen.cs.

Xamarin

Now, write the following code . 
 
IScreen.cs
  1. using System.Threading.Tasks;  
  2. namespace XamarinFormsScreenShot {  
  3.     public interface IScreen {  
  4.         Task < byte[] > CaptureScreenAsync();  
  5.     }  
  6. }  
Implementation per platform - Android Implementation

Go to Solution—>Android —>Right click—>New—>Class—> ScreenHelper.cs
 
Xamarin

Now, write the following code for Capture Current Screen.

Xamarin

ScreenHelper.cs
  1. using XamarinFormsScreenShot.Droid;  
  2. using Android.Graphics;  
  3. using System.IO;  
  4. [assembly: Xamarin.Forms.Dependency(typeof(ScreenHelper))]  
  5. namespace XamarinFormsScreenShot.Droid {  
  6.     public class ScreenHelper: IScreen {  
  7.         public async Task < byte[] > CaptureScreenAsync() {  
  8.             var activity = Xamarin.Forms.Forms.Context as MainActivity;  
  9.             if (activity == null) {  
  10.                 return null;  
  11.             }  
  12.             var view = activity.Window.DecorView;  
  13.             view.DrawingCacheEnabled = true;  
  14.             Bitmap bitmap = view.GetDrawingCache(true);  
  15.             byte[] bitmapData;  
  16.             using(var stream = new MemoryStream()) {  
  17.                 bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);  
  18.                 bitmapData = stream.ToArray();  
  19.             }  
  20.             return bitmapData;  
  21.         }  
  22.     }  
  23. }  
Call to DependencyService

In this step, call the DependencyService for your PCL.
 
Xamarin
MainPage.Xaml.cs
  1. using Xamarin.Forms;  
  2. namespace XamarinFormsScreenShot {  
  3.     public partial class MainPage: ContentPage {  
  4.         public MainPage() {  
  5.             InitializeComponent();  
  6.             imgBanner.Source = ImageSource.FromResource("XamarinFormsScreenShot.images.banner.png");  
  7.             imgCaptured.Source = ImageSource.FromResource("XamarinFormsScreenShot.images.default.jpg");  
  8.         }  
  9.         private async void btnCapture_Clicked(object sender, EventArgs e) {  
  10.             //Call Dependency Service    
  11.             var imageByte = await DependencyService.Get < IScreen > ().CaptureScreenAsync();  
  12.             imgCaptured.Source = ImageSource.FromStream(() => new MemoryStream(imageByte));  
  13.         }  
  14.     }  
  15. }  
Xamarin
 
Click the Play button to try it out.

Xamarin

I hope you have understood how to Capture ScreenShot Using DependencyService.

Thanks for reading. Please share comments and feedback.


Similar Articles