问题
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