Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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



What I have tried:

<pre>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...

Here is my MainPage.xaml file:

XML
<pre><?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>


And here's my MainPage.xaml.cs

C#
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");
        }
    }
}
Posted
Updated 31-May-22 21:28pm
v2

1 solution

Once you have it as a PNG file, it's already binary data - just read the file into a byte array and you are good to go.

But ... OpenReadAsync returns a Stream - so why not just use the data from that directly instead of going via a file? Have you tried the MemoryStream.GetBuffer method[^] - I assume that the Stream is in fact a MemoryStream, but if it isn't it will still support the Stream.Read method[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900