我正在为 Xamarin 表单使用签名板,并且正在尝试将签名设置为图像源。
我正在尝试将流转换为 ByteArray,但转换后的 bytearray 始终为空。
在函数 ReadFully 我尝试了两种方法,但都返回 0 字节
你能否建议我哪里出错了。
我在互联网上查找了这些链接
https://forums.xamarin.com/discussion/19853/load-image-form-byte-array
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Radical.Views.DocketSignaturePad"
xmlns:acr="clr-namespace:Acr.XamForms.SignaturePad;assembly=Acr.XamForms.SignaturePad"
Title="DocketSignaturePad">
<ScrollView>
<StackLayout Padding="10">
<acr:SignaturePadView
x:Name="padView"
HeightRequest="320"
WidthRequest="240"
BackgroundColor="White"
CaptionText="Caption This"
CaptionTextColor="Black"
ClearText="Clear Me!"
ClearTextColor="Red"
PromptText="Prompt Here"
PromptTextColor="Red"
SignatureLineColor="Aqua"
StrokeColor="Black"
StrokeWidth="2"
/>
<Button Clicked="OnChangeTheme" Text="Change Theme" />
<Button Clicked="SaveSignature" Text="Save signature"/>
<Image x:Name="signatureImage" WidthRequest="300" HeightRequest="100" BackgroundColor="Blue"/>
</StackLayout>
</ScrollView>
</ContentPage>
Xaml.cs
async void SaveSignature(object sender, EventArgs e)
{
List<DrawPoint> signaturePoints = padView.GetDrawPoints().ToList();
if (signaturePoints.Count > 0)
{
Stream imageAsBytes = new MemoryStream(ReadFully(padView.GetImage(ImageFormatType.Jpg)));
signatureImage.Source = ImageSource.FromStream(() => new MemoryStream(ReadFully(padView.GetImage(ImageFormatType.Jpg))));
}
}
public static byte[] ReadFully(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
//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();
//}
}