Introduction
Last time, we performed the workstation's sleep functionality on the basis of the client app SLP2 command. In this second and final part of my article series, we will complete the functionality of both of our applications, client and server. First, we will talk about how the screenshot functionality will perform because the client will only send the TSC1 command and the server will receive this. Then, on the basis of this command, the TakeScreenshot function that we will write inside the server app will perform.
After capturing the screenshot of the current activity of the workstation, we will send it to the client. The size of the picture could be large and we cannot send the large data to the client. Therefore, to achieve this target, we will convert the screenshot data into byte-type packets and then send it to the client. When the client will receive the byte-type packets, it will have to convert into its original form. We will also perform the shutdown functionality. If a client sends SHTD3 command to the server application, the server will execute the shutdown function.
Server Application
Let's go back to the server application and write the functionality of screenshot and shutdown workstation. Open the program.cs file and write the following code inside the while loop, that we had implemented in the last episode. In the while loop, we put the try-catch block and inside, we wrote the condition of If-Else. After if sleep check, you will write the following code.
- else if (data.ToUpper().Contains("SHTD3")) {
- Console.WriteLine("Pc is going to Shutdown!" + " \n");
- Shutdown();
- } else if (data.ToUpper().Contains("TSC1")) {
- Console.WriteLine("Take Screenshot!" + " \n");
- var bitmap = SaveScreenshot();
- var stream = new MemoryStream();
- bitmap.Save(stream, ImageFormat.Bmp);
- sendData(stream.ToArray(), client.GetStream());
- }
Shutdown and TakeScreenshot Functions
Add the following namespaces to the server application. Otherwise, Visual Studio will not recognize the graphics keyword of the screenshot function.
- using System.Drawing;
- using System.Drawing.Imaging;
Go back to program.cs and put these functions inside the main class.
- //Workstation Shutdown function
- void Shutdown() {
- System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
- }
- //Save Screenshot function
- Bitmap SaveScreenshot() {
- var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
- // Create a graphics object from the bitmap.
- var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
- // Take the screenshot from the upper left corner to the right
- bottom corner.
- gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
- return bmpScreenshot;
- }
- //Convert Image to byte type data.
- void sendData(byte[] data, NetworkStream stream) {
- int bufferSize = 1024;
- byte[] dataLength = BitConverter.GetBytes(data.Length);
- stream.Write(dataLength, 0, 4);
- int bytesSent = 0;
- int bytesLeft = data.Length;
- while (bytesLeft > 0) {
- int curDataSize = Math.Min(bufferSize, bytesLeft);
- stream.Write(data, bytesSent, curDataSize);
- bytesSent += curDataSize;
- bytesLeft -= curDataSize;
- }
- }
We have finished our server application and all its functionality. Let's start to code our client application.
Client Application
Step 5 - Create Operations Page
Go back to the client application (Xamarin.Forms) and add new ContentPage with name OperationsPage. Inside this layout, add the following code to create more buttons and picture holders.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:XamarinForms.Client"
- x:Class="XamarinForms.Client.OperationsPage">
- <ContentPage.Content>
- <StackLayout Orientation="Vertical">
- <Button x:Name="Screenshot" Text="Screenshot" Clicked="Screenshot_Clicked"/>
- <Button x:Name="Sleep" Text="Sleep" Clicked="Sleep_Clicked"/>
- <Button x:Name="Shutdown" Text="Shutdown" Clicked="Shutdown_Clicked"/>
- <Image x:Name="imageView"/>
- </StackLayout>
- </ContentPage.Content>
- </ContentPage>



Join the conversation! Your thoughts help the community grow.