User signature in universal windows 8.1 app

…衆ロ難τιáo~ 提交于 2020-01-17 11:37:53

问题


Is there any ability to sign the document on windows 8.1 mobile device? Something like a canvas where user will be able to draw your signature by hand or stylus. Is there any XAML control for this task or something else ?


回答1:


I struggled with this for the best part of a day. It seems that the built-in inking control that used to be available in Windows Phone 7 and 8.0 was pulled out for 8.1

I used the techniques in the article pointed out by Chris W to make a control that can be added to the page's XAML and handle everything else.

The magic is in the WritableBitmap and the PointerPressed and PointerMoved methods.

using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace ProofOfConcept.App.Controls
{
    public class SignatureCaptureControl : Canvas
    {
        private WriteableBitmap _writeableBitmap;

        private Point _currentPoint;
        private Point _oldPoint;

        public SignatureCaptureControl()
        {
            _writeableBitmap = new WriteableBitmap(300, 130);
            PointerPressed += SignatureCaptureControl_PointerPressed;
            PointerMoved += SignatureCaptureControl_PointerMoved;
        }

        private void SignatureCaptureControl_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            _currentPoint = e.GetCurrentPoint(this).Position;
            _oldPoint = _currentPoint;
        }

        void SignatureCaptureControl_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            _currentPoint = e.GetCurrentPoint(this).Position;
            _writeableBitmap.DrawLine((int)_currentPoint.X, (int)_currentPoint.Y, (int)_oldPoint.X, (int)_oldPoint.Y, PenColor);
            this.InvalidateArrange();
            _oldPoint = _currentPoint;
        }

        public void ClearSignature()
        {
            _writeableBitmap.Clear();
        }

    }

}

Using the following The control will display a 300x130 white box that will draw lines as the finger or stylus is dragged over it.

<Border Background="White">
    <controls:SignatureCaptureControl Height="130" Width="300"/>
</Border>

To get an image out we have to extract the pixel data from the WritableBitmap and then encode it as a Bitmap/Gif/Jpeg/Png/etc.

public Task<byte[]> ReadAsPngImageAsync()
{
    return ReadImageFromWritableBitmapAsync(_writeableBitmap, Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId);
}


internal static async Task<byte[]> ReadImageFromWritableBitmapAsync(Windows.UI.Xaml.Media.Imaging.WriteableBitmap writeableBitmap, Guid encoderId)
{
    var rawPixels = await ConvertBitmapToByteArrayAsync(writeableBitmap);
    var encodedPixels = await EncodePixels(rawPixels, encoderId, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight);
    return encodedPixels;
}

private static async Task<byte[]> ConvertBitmapToByteArrayAsync(Windows.UI.Xaml.Media.Imaging.WriteableBitmap bitmap)
{
    using (var stream = bitmap.PixelBuffer.AsStream())
    {
        var pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);
        return pixels;
    }
}

private static async Task<byte[]> EncodePixels(byte[] signaturePixels, Guid encoderId, uint pixelWidth, uint pixelHeight)
{
    using (var randomAccessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
    {
        var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(encoderId, randomAccessStream);
        encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied,
            pixelWidth, pixelHeight,
            96, 96, signaturePixels);
        await encoder.FlushAsync();

        using (var stream = randomAccessStream.GetInputStreamAt(0))
        {
            var pixels = new byte[(uint)randomAccessStream.Size];
            stream.AsStreamForRead().Read(pixels, 0, pixels.Length);
            return pixels;
        }
    }
}

Now we have a byte array representing the image in a standard form we can simply save the data to local storage or submit it to a service.



来源:https://stackoverflow.com/questions/27254745/user-signature-in-universal-windows-8-1-app

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