儲存筆墨
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