Search code examples
c#xamarinxamarin.formssignaturepad

Xamarin forms signature pad Get image


Im using the following code to get the signature from the signature pad, But any value doesn't come.

try {
            var signature = padView.GetImage (Acr.XamForms.SignaturePad.ImageFormatType.Png);
            using (BinaryReader br = new BinaryReader (signature)) {
                var result = br.ReadBytes ((int)signature.Length);

            }

        } catch (Exception ex) {
            //              Helper_ErrorHandling.SendErrorToServer (ex);
        }

Am I ding it wrong, Also how do i convert this to a base64 string ?


Solution

  • I'm not too familiar with the Xamarin Forms Signature Pad, but if you're looking for a way to convert a Stream to as base64 string, try this:

    [...]
    string base64String;
    using (var memoryStream = new MemoryStream()) 
    { 
        signature.CopyTo( memoryStream ); 
        var byteArray = memoryStream.ToArray();
        base64String = Convert.ToBase64String( byteArray );
    } 
    

    EDIT: you can most of the time skip the copy, if you check if signature is already a MemoryStream...

    [...]
    string base64String;
    var signatureMemoryStream = signature as MemoryStream;
    if (signatureMemoryStream == null)
    {
        signatureMemoryStream = new MemoryStream();
        signature.CopyTo( signatureMemoryStream );
    }
    var byteArray = signatureMemoryStream.ToArray();
    base64String = Convert.ToBase64String( byteArray );