Windows Phone 8.1中的签名捕获

时间:2017-04-28 03:55:00

标签: xaml canvas windows-phone-8.1 writablebitmap

我很惊讶我无法在Windows Phone 8.1中找到任何捕获签名的解决方案。到目前为止,我在StackOverflow here...中只发现了一个,但它抱怨WriteableBitmap.DrawLine方法不可用。

以上链接中的代码:

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

2 个答案:

答案 0 :(得分:1)

此代码示例需要WriteableBitmapEx库,DrawLine方法被定义为该库中的扩展方法。

您可以在NuGet包管理器中安装此包。

enter image description here

答案 1 :(得分:0)

最后,我找到了一个完全符合我要求的示例代码。它确实让用户在画布上涂写了他们的签名或其他任何东西,我还没弄明白如何保存图像并将其上传到网络服务。我稍后会谈到的。但这是创建绘图板的代码。

Link到我找到代码的网站。

我把它作为UserControl。

这就是我使用UserControl的方式:

<Grid x:Name="inkPanel">
    <Controls:SignatureCaptureControl/>
</Grid>

UserControl Xaml:

<UserControl
    x:Class="MyApp.Controls.SignatureCaptureControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="170"></RowDefinition>
        <RowDefinition Height="60"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>
    <Canvas x:Name="inkCanvas" Grid.Row="0">
        <Rectangle x:Name="rect" VerticalAlignment="Top" Height="150" Width="360" Fill="#FFF4F4F5" Stroke="Black" />
    </Canvas>
    <PasswordBox x:Name="txtPin" Grid.Row="1" Width="360" PlaceholderText="Enter Pin">
    </PasswordBox>
    <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="0,10,0,0" HorizontalAlignment="Center">
        <Button Name="btnAccept" Content="Accept" Click="btnAccept_Click"></Button>
        <Button Name="btnClear" Content="Clear" Click="btnClear_Click"></Button>
    </StackPanel>
</Grid>
</UserControl>

代码背后:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace MyApp.Controls
{
    public sealed partial class SignatureCaptureControl : UserControl
    {
        Windows.UI.Xaml.Shapes.Path _Current;

        public SignatureCaptureControl()
        {
            this.InitializeComponent();
            inkCanvas.PointerPressed += inkCanvas_PointerPressed;
            inkCanvas.PointerMoved += inkCanvas_PointerMoved;
            inkCanvas.PointerReleased += inkCanvas_PointerReleased;
            inkCanvas.PointerExited += inkCanvas_PointerReleased;
        }

        void inkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            _Current = null;
        }

        void inkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            var pointer = e.GetCurrentPoint(inkCanvas);
            if (!pointer.Properties.IsLeftButtonPressed || _Current == null)
                return;
            var segments = (_Current.Data as PathGeometry).Figures.First().Segments.First() as PolyLineSegment;
            segments.Points.Add(pointer.Position);
        }

        void inkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            var pointer = e.GetCurrentPoint(inkCanvas);
            if (!pointer.Properties.IsLeftButtonPressed)
                return;

            _Current = new Windows.UI.Xaml.Shapes.Path
            {
                Data = new PathGeometry
                {
                    Figures = { new PathFigure { StartPoint = pointer.Position, Segments = { new PolyLineSegment() } } }
                },
                Stroke = new SolidColorBrush(Colors.Black),
                StrokeThickness = 4d,
            };

            inkCanvas.Children.Add(_Current);
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            inkCanvas.Children.Clear();
            inkCanvas.Children.Add(rect);
            txtPin.Password = "";
        }

        private void btnAccept_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}