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

本文关键字:用户 应用 Windows 通用 | 更新日期: 2023-09-27 18:32:46

是否可以在 Windows 8.1 移动设备上对文档进行签名?类似于画布的东西,用户可以在其中通过手写笔或手写笔绘制您的签名。是否有此任务或其他内容的 XAML 控件?

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

我为此

挣扎了一天的大部分时间。似乎曾经在Windows Phone 7和8.0中可用的内置墨迹书写控件在8.1中被拉了出来

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

神奇之处在于 WritableBitmap 和 PointerPressed 和 PointerMoving 方法。

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>

为了得到一个图像,我们必须从可写位图中提取像素数据,然后将其编码为位图/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;
        }
    }
}

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