0

I am working on a Xamarin Forms project in which I want to transform my screenshot which is .PNG, to binary and upload it to a server.

Now the server part requires an API which I will be handed over by someone who has already made it, and I will only have to implement it... after I complete this task

I have so far succeeded in taking a screenshot of the whole screen and saving it into my device, so, I only need a bit of help with the Conversion part.. I have found a code for a solution but due to lack of knowledge and experience in C# I failed to implement it...

This is MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="UploadingFiles.MainPage">

    <StackLayout>
    <Button Text="Document filled"                          
        FontSize="12.5"
        Margin="0,0,0,0"
        HorizontalOptions="Center"
        FontFamily="MYICON"
        BorderColor="Black"
        Padding="0,0,0,0"
        BorderWidth="1"
        BackgroundColor="White"
        WidthRequest="120"
        HeightRequest="23"
        CornerRadius="0"
        Clicked="DoPDFThings"
        TextColor="Black"
        FontAttributes="Bold"/>

    </StackLayout>

</ContentPage>

Here is the C# part...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NativeMedia;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace UploadingFiles
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void DoPDFThings(object sender, EventArgs e)
        {
            var screenshot = await Screenshot.CaptureAsync(); //    working 100% for partial sc: was var imageStream = await header.CaptureImageAsync();

            //  working 100% for partial sc: await MediaGallery.SaveAsync(MediaFileType.Image, imageStream, "myScreenshot.png");

            //  var screenshot = await Screenshot.CaptureAsync();

            await MediaGallery.SaveAsync(MediaFileType.Image, await screenshot.OpenReadAsync(), "myScreenshot.png");
        }
    }
}
Charbel
  • 15
  • 4

1 Answers1

1

You can try to convert the stream you have to MemoryStream and use the MemoryStream.ToArray method. Such as:

 var screenshot = await Screenshot.CaptureAsync(); 
 var byte[] array;
 using (MemoryStream ms = new MemoryStream())
 {
    screenshot.CopyTo(ms);
    array = ms.ToArray();
 }

Or you want to get the array from a local file:

 byte[] buffer = System.IO.File.ReadAllBytes("");//provide the path of the .PNG in the ("")
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Will try this. just need to find the path of image in my native gallery – Charbel Jun 01 '22 at 10:15
  • Has the problem been solved?@Charbel – Liyun Zhang - MSFT Jun 02 '22 at 00:52
  • still trying to find the path to my image, im very slow haha, when i do and it works I'll give u the Nike check – Charbel Jun 03 '22 at 06:41
  • do u have any tips on finding the path? I set it to: " /Users/charbelmansour/.android/avd/tablet_m-dpi_10_1in_pie_9_0_-_api_28.avdsnapshots/default_boot/myScreenshot.png") " to no avail – Charbel Jun 03 '22 at 09:25
  • You need to copy the file into the simulator, you can check the answer in this [link](https://stackoverflow.com/questions/72344848/slash-xamarin-issue/72346345#72346345). @Charbel – Liyun Zhang - MSFT Jun 06 '22 at 04:26