1

我使用以下代码从签名板获取签名,但没有任何值。

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);
        }

我是不是错了,我如何将它转换为 base64 字符串?

4

2 回答 2

2

我对 Xamarin Forms Signature Pad 不太熟悉,但如果您正在寻找一种将 a 转换Stream为 base64 字符串的方法,请尝试以下操作:

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

编辑:如果您检查是否signature已经是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 );
于 2016-06-03T12:08:24.457 回答
0

使用最新的 PCL 兼容 Xamarin 包:

acr-xamarin 形式

这种方法就像一个魅力!

private string ConvertSignatureToBase64()
    {
        try
        {
            byte[] data;
            if(Device.OS == TargetPlatform.iOS)
            {
                var img = SignaturePad.GetImage(Acr.XamForms.SignaturePad.ImageFormatType.Jpg);
                var signatureMemoryStream = new MemoryStream();
                img.CopyTo(signatureMemoryStream);
                data = signatureMemoryStream.ToArray();
            }
            else
            {
                var img = SignaturePad.GetImage(Acr.XamForms.SignaturePad.ImageFormatType.Jpg);
                var signatureMemoryStream = (MemoryStream)img;
                data = signatureMemoryStream.ToArray();
            }

            return Convert.ToBase64String(data);      
        }
        catch(Exception ex)
        {
            return ex.ToString();
        }
    }
于 2017-05-04T18:53:46.490 回答