通用Windows 8.1应用程序中的用户签名

时间:2014-12-02 16:53:09

标签: c# .net xaml windows-8.1

是否有能力在Windows 8.1移动设备上签署文档?像画布一样的东西,用户可以用手或手写笔绘制你的签名。是否有任何XAML控件用于此任务或其他什么?

1 个答案:

答案 0 :(得分:0)

我在这一天的最佳时刻挣扎着。似乎以前在Windows Phone 7和8.0中可用的内置墨迹控件已被拉出8.1

我使用了 Chris W 指出的文章中的技术来制作一个可以添加到页面的XAML并处理其他所有内容的控件。

魔术在WritableBitmap以及PointerPressed和PointerMoved方法中。

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();
        }

    }

}

使用以下控件将显示一个300x130的白色框,当手指或手写笔被拖过它时将绘制线条。

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

要获取图像,我们必须从WritableBitmap中提取像素数据,然后将其编码为Bitmap / Gif / Jpeg / Png /等。

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;
        }
    }
}

现在我们有一个以标准形式表示图像的字节数组,我们只需将数据保存到本地存储或将其提交给服务。