Articles in this section
Category / Section

How to save signature as a byte array in Xamarin.Forms Signature Pad

1 min read

Step 1: Create a SfSignaturePad sample that includes all required assemblies.

 

Please refer to the following link to create a simple SignaturePad sample along with the ways to configure it.

https://help.syncfusion.com/xamarin/signaturepad/overview

 

Step 2: Create a simple SignaturePad sample using the following code snippet.

 

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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:signature="clr-namespace:Syncfusion.XForms.SignaturePad;assembly=Syncfusion.SfSignaturePad.XForms"
             x:Class="SignaturePadSample.MainPage">
 
    <StackLayout>
        <Label Text="Input Your Signature"/>
        <Frame>
            <signature:SfSignaturePad   x:Name="signature"
                                      HeightRequest="250"/>
        </Frame>
        <Button Text="ConvertSourceToBytes" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

 

Step 3: You can convert the saved SignaturePad bitmap format to byte arrays as shown in the following code sample.

 

XAML.cs:

using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
 
namespace SignaturePadSample
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void Button_Clicked(object sender, EventArgs e)
        {
            signature.Save();
            StreamImageSource streamImageSource = (StreamImageSource)signature.ImageSource;
            System.Threading.CancellationToken cancellationToken =
            System.Threading.CancellationToken.None;
            Task<Stream> task = streamImageSource.Stream(cancellationToken);
            Stream stream = task.Result;
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
        }
    }
}

 

View Sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied