■ InkCanvas 클래스에서 ISF(Ink Serialized Format) 파일을 로드하고 저장하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Border Grid.Row="0" BorderThickness="2" BorderBrush="Black"> <InkCanvas Name="inkCanvas" /> </Border> <StackPanel Grid.Row="1" HorizontalAlignment="Center" Margin="10" Orientation="Horizontal"> <Button Name="loadButton" Width="100" Height="30" Content="로드하기" /> <Button Name="saveButton" Margin="10 0 0 0" Width="100" Height="30" Content="저장하기" /> <Button Name="clearButton" Margin="10 0 0 0" Width="100" Height="30" Content="지우기" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
using System.IO; using System.Windows; using System.Windows.Ink; using Microsoft.Win32; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 열기 대화 상자 /// </summary> private OpenFileDialog openFileDialog; /// <summary> /// 파일 저장하기 대화 상자 /// </summary> private SaveFileDialog saveFileDialog; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.openFileDialog = new OpenFileDialog(); this.openFileDialog.Filter = "isf 파일 (*.isf)|*.isf"; this.saveFileDialog = new SaveFileDialog(); this.saveFileDialog.Filter = "isf 파일 (*.isf)|*.isf"; this.loadButton.Click += loadButton_Click; this.saveButton.Click += saveButton_Click; this.clearButton.Click += clearButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 로드하기 버튼 클릭시 처리하기 - loadButton_Click(sender, e) /// <summary> /// 로드하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void loadButton_Click(object sender, RoutedEventArgs e) { if(this.openFileDialog.ShowDialog() == true) { using(FileStream fileStream = new FileStream(this.openFileDialog.FileName, FileMode.Open)) { this.inkCanvas.Strokes = new StrokeCollection(fileStream); } } } #endregion #region 저장하기 버튼 클릭시 처리하기 - saveButton_Click(sender, e) /// <summary> /// 저장하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveButton_Click(object sender, RoutedEventArgs e) { if(this.saveFileDialog.ShowDialog() == true) { using(FileStream fileStream = new FileStream(this.saveFileDialog.FileName, FileMode.Create)) { this.inkCanvas.Strokes.Save(fileStream); } } } #endregion #region 지우기 버튼 클릭시 처리하기 - clearButton_Click(sender, e) /// <summary> /// 지우기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void clearButton_Click(object sender, RoutedEventArgs e) { this.inkCanvas.Strokes.Clear(); } #endregion } } |