Signature Pad Xamarin.Forms Saving signature as a file

可紊 提交于 2019-12-25 01:09:39

问题


I am a beginner in Xamarin. I tried to write a simple app to save the signature with the help of Signature Pad. A piece of code from MainPage.xaml

<controls:SignaturePadView x:Name="SignaturePAD"
                           Grid.Row="1" 
                           StrokeColor="Black" 
                           StrokeWidth="3"
                           BackgroundColor="Gray" 
                           CaptionTextColor="Black"
                           PromptTextColor="Black"
                           SignatureLineColor="Black"
                           CaptionText="Podpis odbiorcy">
</controls:SignaturePadView>

<Button Grid.Row="2" 
        x:Name="SaveButton"
        Text="Potwierdź" 
        Clicked="SaveSignature"/>

and a fragment from MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    public async void SaveSignature(object sender, EventArgs e)
    {
        Stream image = await SignaturePAD.GetImageStreamAsync(SignatureImageFormat.Png);

    }

}

And my question is how can I save it into the my phone gallery? I will be grateful for any help


回答1:


You might be able to find a plugin somewhere that would do this for you, but I would convert the stream to a byte[] like so:

using (image)
using (var memoryStream = new MemoryStream())
{
    await image.CopyToAsync(memoryStream);
    var picture = memoryStream.ToArray();
}

Then convert the byte[] to the native picture on each platform.

For Android use:

BitmapFactory.DecodeByteArray(picture, 0, picture.Length);

And for iOS use:

var nsData = NSData.FromArray(picture);
var uiImage = UIImage.LoadFromData(nsData);

Then you would need to add the image to the underlying gallery. Here is an example of how to achieve something like that on iOS: https://developer.xamarin.com/recipes/ios/media/video_and_photos/save_photo_to_album_with_metadata/

For Android, this post might help: Xamarin.Forms Android save image to gallery




回答2:


the SignaturePad returns a stream - so you can write it to a file using normal C# I/O, like FileStream

Stream image = await SignaturePAD.GetImageStreamAsync(SignatureImageFormat.Png);

using (FileStream file = new FileStream(file_path, FileMode.Create, System.IO.FileAccess.Write))
{
  image.CopyTo(file);
}


来源:https://stackoverflow.com/questions/54907793/signature-pad-xamarin-forms-saving-signature-as-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!