存储墨迹

Save 方法支持将墨迹存储为墨迹序列化格式 (ISF)。 StrokeCollection 类的构造函数为读取墨迹数据提供支持。

墨迹存储和检索

本部分讨论如何在 WPF 平台中存储和检索墨迹。

以下示例实现一个按钮单击事件处理程序,它向用户显示“文件保存”对话框,并从 InkCanvas 输出墨迹保存到文件中。

private void buttonSaveAsClick(object sender, RoutedEventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "isf files (*.isf)|*.isf";

    if (saveFileDialog1.ShowDialog() == true)
    {
        FileStream fs = new FileStream(saveFileDialog1.FileName,
                                       FileMode.Create);
        theInkCanvas.Strokes.Save(fs);
        fs.Close();
    }
}
Private Sub buttonSaveAsClick(ByVal sender As Object, ByVal e As RoutedEventArgs) 

    Dim saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.Filter = "isf files (*.isf)|*.isf"

    If saveFileDialog1.ShowDialog() Then
        Dim fs As New FileStream(saveFileDialog1.FileName, FileMode.Create)
        theInkCanvas.Strokes.Save(fs)
        fs.Close()
    End If

End Sub

以下示例实现一个按钮单击事件处理程序,它向用户显示“文件打开”对话框,并读取来自文件的墨迹到 InkCanvas 元素中。

private void buttonLoadClick(object sender, RoutedEventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "isf files (*.isf)|*.isf";

    if (openFileDialog1.ShowDialog() == true)
    {
        FileStream fs = new FileStream(openFileDialog1.FileName,
                                       FileMode.Open);
        theInkCanvas.Strokes = new StrokeCollection(fs);
        fs.Close();
    }
}
Private Sub buttonLoadClick(ByVal sender As Object, ByVal e As RoutedEventArgs) 

    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.Filter = "isf files (*.isf)|*.isf"

    If openFileDialog1.ShowDialog() Then
        Dim fs As New FileStream(openFileDialog1.FileName, FileMode.Open)
        theInkCanvas.Strokes = New StrokeCollection(fs)
        fs.Close()
    End If

End Sub

另请参阅