■ XpsDocument 클래스를 사용해 XPS 문서 이미지를 저장하는 방법을 보여준다.
▶ 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<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="XpsDocument 클래스 : XPS 문서 이미지 저장하기" FontFamily="나눔고딕코딩" FontSize="16" Loaded="Window_Loaded"> <Grid Margin="5"> <Grid.Resources> <Style TargetType="TextBox"> <Setter Property="Margin" Value="5" /> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="XPS 파일" /> <TextBox Name="filePathTextBox" Grid.Row="0" Grid.Column="1" Text="Test.xps" /> <Button Name="convertButton" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="5" Width="100" Height="30" IsDefault="True" Content="변환하기" Click="convertButton_Click" /> <Label Grid.Row="2" Grid.Column="0" Content="스케일" /> <ComboBox Name="cboScale" Grid.Row="2" Grid.Column="1" Width="60" Height="25" HorizontalAlignment="Left" SelectionChanged="scaleComboBox_SelectionChanged"> <ComboBoxItem Content="25%" /> <ComboBoxItem Content="50%" /> <ComboBoxItem Content="75%" /> <ComboBoxItem Content="100%" IsSelected="True" /> <ComboBoxItem Content="150%" /> <ComboBoxItem Content="200%" /> <ComboBoxItem Content="300%" /> </ComboBox> <TabControl Name="resultTabControl" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="5" Visibility="Hidden" /> </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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Xps.Packaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이미지 스케일 /// </summary> private double imageScale = 1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.filePathTextBox.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.xps"); } #endregion #region 변환하기 버튼 클릭시 처리하기 - convertButton_Click(sender, e) /// <summary> /// 변환하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void convertButton_Click(object sender, RoutedEventArgs e) { Cursor = Cursors.Wait; this.resultTabControl.Visibility = Visibility.Hidden; int pageCount = SaveXPSImage(this.filePathTextBox.Text); ShowXPSImage(this.filePathTextBox.Text, pageCount); FitImageSize(); this.resultTabControl.Visibility = Visibility.Visible; Cursor = null; } #endregion #region 스케일 콤보 박스 선택 변경시 처리하기 - scaleComboBox_SelectionChanged(sender, e) /// <summary> /// 스케일 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem comboBoxItem = this.cboScale.SelectedItem as ComboBoxItem; string scaleString = comboBoxItem.Content.ToString(); scaleString = scaleString.Replace("%", ""); this.imageScale = double.Parse(scaleString) / 100.0; FitImageSize(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region XPS 이미지 저장하기 - SaveXPSImage(xpsFilePath) /// <summary> /// XPS 이미지 저장하기 /// </summary> /// <param name="xpsFilePath">XPS 파일 경로</param> /// <returns>페이지 카운트</returns> private int SaveXPSImage(string xpsFilePath) { if(!xpsFilePath.ToLower().EndsWith(".xps")) { throw new ArgumentException("파일 확장자가 .xps가 아닙니다."); } string fileName = xpsFilePath.Substring(0, xpsFilePath.Length - 4); XpsDocument xpsDocument = new XpsDocument(xpsFilePath, FileAccess.Read); IDocumentPaginatorSource documentPaginatorSource = xpsDocument.GetFixedDocumentSequence(); DocumentPaginator documentPaginator = documentPaginatorSource.DocumentPaginator; int pageCount = documentPaginator.PageCount; for(int i = 0; i < pageCount; i++) { using(DocumentPage documentPage = documentPaginator.GetPage(i)) { int pageWidth = (int)documentPage.Size.Width; int pageHeight = (int)documentPage.Size.Height; RenderTargetBitmap bitmap = new RenderTargetBitmap ( pageWidth, pageHeight, 96, 96, PixelFormats.Default ); bitmap.Render(documentPage.Visual); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); using(MemoryStream stream = new MemoryStream()) { encoder.Save(stream); using(FileStream fileStream = new FileStream(fileName + (i + 1).ToString() + ".png", FileMode.Create)) { fileStream.Write(stream.GetBuffer(), 0, (int)stream.Length); fileStream.Close(); } } } } return pageCount; } #endregion #region XPS 이미지 표시하기 - ShowXPSImage(xpsFilePath, pageCount) /// <summary> /// XPS 이미지 표시하기 /// </summary> /// <param name="xpsFilePath">XPS 파일 경로</param> /// <param name="pageCount">페이지 카운트</param> private void ShowXPSImage(string xpsFilePath, int pageCount) { string fileName = xpsFilePath.Substring(0, xpsFilePath.Length - 4); this.resultTabControl.Items.Clear(); for(int i = 1; i <= pageCount; i++) { Image image = new Image(); image.Source = new BitmapImage(new Uri(fileName + i.ToString() + ".png", UriKind.Absolute)); ScrollViewer scrollViewer = new ScrollViewer(); scrollViewer.Background = Brushes.Black; scrollViewer.Content = image; scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; TabItem tabItem = new TabItem(); this.resultTabControl.Items.Add(tabItem); tabItem.Header = "페이지 " + i.ToString(); tabItem.Content = scrollViewer; } } #endregion #region 이미지 크기 맞추기 - FitImageSize() /// <summary> /// 이미지 크기 맞추기 /// </summary> private void FitImageSize() { if(this.resultTabControl == null) { return; } foreach(TabItem tabItem in this.resultTabControl.Items) { ScrollViewer scrollViewer = tabItem.Content as ScrollViewer; Image image = scrollViewer.Content as Image; image.Width = image.Source.Width * this.imageScale; image.Height = image.Source.Height * this.imageScale; } } #endregion } } |