流到ByteArray-将签名板转换为图像 [英] Stream to ByteArray - Convert Signature pad to Image
本文介绍了流到ByteArray-将签名板转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为Xamarin表单使用签名板,并且试图将签名设置为图像源.
I am using a Signature pad for Xamarin forms and I am trying to set the signature to an image source.
我正在尝试将流转换为ByteArray,但转换后的bytearray始终为空.
I am trying to convert stream to ByteArray but the converted bytearray is always empty.
在ReadFully函数中,我尝试了两种方法,但是都返回了0个字节
In the function ReadFully I have tried two approaches but both returned 0 bytes
请问我要去哪里错了.
我在互联网上查找了这些链接
I looked up on the internet found these links
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();
//}
}
推荐答案
您可以使用此代码,它对我有用:
You can use this code, it worked for me:
Stream image = await sign.GetImageStreamAsync(SignatureImageFormat.Png);
img_signature.Source = ImageSource.FromStream(() => image);
这篇关于流到ByteArray-将签名板转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文