Forum Xamarin.Forms

getting jpg from users gallery

i need to get a photo from a users gallery and atm i do so like this

private async void addprofilepicture_Clicked(object sender, EventArgs e)

        Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();

        if (stream != null)
            ImageSource Image;
            byte[] b = ReadFully(stream);
            String s = Convert.ToBase64String(b);
            Image = Xamarin.Forms.ImageSource.FromStream(
            () => new MemoryStream(Convert.FromBase64String(s)));
            imagebutton.Source = Image;
            basse64.Text = (stream).ToString();    


    public static byte[] ReadFully(Stream input)
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                ms.Write(buffer, 0, read);
            return ms.ToArray();
    public async void Postbase(byte[] bytearray) {

        base64s s = new base64s()
            base64string = bytearray

        string stringpayload =  JsonConvert.SerializeObject(s);

        await myAPI.PostProfilePhoto(s);

  public  class base64s { 
      public byte[] base64string { get; set; }


as you can see i get a byte[] and send it but it takes so long to send that that application will crash waiting and my backend will list a very long string to the console for minutes. obviously users shouldnt have to wait this long to send a picture. so how to a get a jpg or maybe even png to send from users gallery. all i find on microsoft sites is how to get it from drawable but direct me to right place if there is a tutorial on this.


Sign In or Register to comment.