using Comal.Classes; using InABox.Clients; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SignatureSaver { public delegate void SignatureSaved(); public event SignatureSaved OnSignatureSaved; public SignatureSaver () { InitializeComponent (); } void Save_Clicked(object sender, EventArgs e) { signaturePad.Save(); if (signaturePad.ImageSource != null) { string base64 = ImageSourceToBase64(signaturePad.ImageSource); byte[] data = Convert.FromBase64String(base64); try { Employee employee = new Employee { ID = App.Data.Me.ID }; employee.Signature = data; new Client().Save(employee, "Signature Saved from mobile"); } catch { } if (Application.Current.Properties.ContainsKey("SavedSignature")) { Application.Current.Properties["SavedSignature"] = data; } else { Application.Current.Properties.Add("SavedSignature", data); } } OnSignatureSaved?.Invoke(); Navigation.PopAsync(); } void Clear_Clicked(object sender, EventArgs e) { signaturePad.Clear(); } void DeleteSaved_Clicked(object sender, EventArgs e) { try { Employee employee = new Employee { ID = App.Data.Me.ID }; employee.Signature = null; new Client().Save(employee, "Signature Saved from mobile"); DisplayAlert("Success", "Signature deleted for Employee", "OK"); } catch { } } private string ImageSourceToBase64(ImageSource source) { StreamImageSource streamImageSource = (StreamImageSource)source; System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None; Task task = streamImageSource.Stream(cancellationToken); Stream stream = task.Result; byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); return s; } } }