첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ InkCanvas 클래스에서 ISF(Ink Serialized Format) 파일을 로드하고 저장하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ MainWindow.xaml

<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

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
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,