Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Overview: I would like to take a screenshot, turn it into a byte array and then upload it to a server. I'm using Xamarin.Forms for Visual Studio Mac.

The target device is an Android Emulator Tablet.

Tried a lot but I'm not that good in C#.

The piece of code I want to implement:

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


The code I want to implement it in:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
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");

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

        }
    }
}


What I have tried:

I have tried to paste it under my code but then I got multiple red underlines, specifically under 'screenshot', 'var', 'byte' and 'CopyTo'.
Posted
Updated 2-Jun-22 22:14pm

1 solution

 
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