想做一个WPF的触摸签名,目前设想是获取触摸坐标然后储存起来,有没有高手给点指导,怎么样获取坐标点数,然后储存起来,做成一个text。有代码最好了!
解决方案
10
在手写触摸屏上签吗,还是专门的设备?
90
可以用InkCanvas来做。例如下例示范笔画的存盘和载入(具体的坐标获取等可以搜索和参考ink serialized format)。
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <InkCanvas Name="inkCanvas" /> <Button Click="ButtonSave_Click" Content="Save" /> <Button Click="ButtonLoad_Click" Content="Load" /> </StackPanel> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ButtonSave_Click(object sender, RoutedEventArgs e) { var dlg = new Microsoft.Win32.SaveFileDialog() { Filter = "strokes|*.strokes"}; if (dlg.ShowDialog(this) == true) { using(var stream = System.IO.File.OpenWrite(dlg.FileName)) { this.inkCanvas.Strokes.Save(stream); } } } private void ButtonLoad_Click(object sender, RoutedEventArgs e) { var dlg = new Microsoft.Win32.OpenFileDialog() { Filter = "strokes|*.strokes" }; if (dlg.ShowDialog(this) == true) { using (var stream = System.IO.File.OpenRead(dlg.FileName)) { this.inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(stream); } } } }
要存成图像也可以,例如:
using (var stream = System.IO.File.OpenWrite(dlg.FileName + ".jpg")) { Size size = new Size(this.inkCanvas.ActualWidth, this.inkCanvas.ActualHeight); RenderTargetBitmap rt = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default); rt.Render(this.inkCanvas); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rt)); encoder.Save(stream); }