■ 이미지 뷰어를 만드는 방법을 보여준다.
▶ 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 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 |
<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"> <Window.Resources> <Style x:Key="HeaderTextBlockKey1" TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="Padding" Value="5" /> <Setter Property="Foreground" Value="White" /> <Setter Property="FontWeight" Value="Bold" /> </Style> <Style x:Key="HeaderTextBlockKey2" TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="Margin" Value="5" /> <Setter Property="Padding" Value="5" /> <Setter Property="FontWeight" Value="Bold" /> </Style> <Style x:Key="Normal" TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> </Style> <Style x:Key="DarkGradientBackgroundStyleKey" TargetType="{x:Type Border}"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0 0" EndPoint="0 1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0" Color="#e5e5e5" /> <GradientStop Offset="1" Color="#000000" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Setter.Value> </Setter> </Style> </Window.Resources> <DockPanel Background="White" LastChildFill="True"> <Border DockPanel.Dock="Left" Margin="10"> <StackPanel HorizontalAlignment="Center"> <StackPanel.Resources> <Style x:Key="ScrollingListBoxKey" TargetType="{x:Type ListBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border BorderThickness ="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"> <ScrollViewer Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <StackPanel Margin="5" IsItemsHost="true" /> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="ThumbnailStyleKey" TargetType="{x:Type Border}"> <Setter Property="Margin" Value="5" /> <Setter Property="Width" Value="100" /> <Setter Property="Height" Value="100" /> <Setter Property="BorderThickness" Value="1" /> </Style> <Style x:Key="ImageListStyleLey"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <DockPanel Margin="5" Width="100"> <Image Source="{Binding Path=FullName}" /> <TextBlock Text="{Binding Path=FullName}" /> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </StackPanel.Resources> <Border Style="{StaticResource DarkGradientBackgroundStyleKey}"> <TextBlock Style="{StaticResource HeaderTextBlockKey1}" Text="이미지 선택 :" /> </Border> <ListBox Name="listBox" Style="{StaticResource ScrollingListBoxKey}" ItemContainerStyle="{StaticResource ImageListStyleLey}" ItemsSource="{Binding}" Height="250" /> <StackPanel Margin="0 10 0 10" Background="#e5e5e5"> <Border Style="{StaticResource DarkGradientBackgroundStyleKey}"> <TextBlock Style="{StaticResource HeaderTextBlockKey1}" Text="메타 데이터" /> </Border> <TextBlock Style="{StaticResource HeaderTextBlockKey2}" Text="이미지 크기" /> <TextBlock Name="imageSizeTextBlock" Style="{StaticResource Normal}" /> <TextBlock Style="{StaticResource HeaderTextBlockKey2}" Text="픽셀 포맷" /> <TextBlock Name="imageFormatTextBlock" Style="{StaticResource Normal}" /> <TextBlock Style="{StaticResource HeaderTextBlockKey2}" Text="파일 크기" /> <TextBlock Name="fileSizeTextBlock" Style="{StaticResource Normal}" Margin="0 0 0 10" /> </StackPanel> </StackPanel> </Border> <Border Width="500" Height="500" BorderThickness="1" BorderBrush="Black"> <Image Name="image" /> </Border> </DockPanel> </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 |
using System; using System.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 정보 리스트 /// </summary> private List<FileInfo> fileInfoList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.listBox.SelectionChanged += listBox_SelectionChanged; } #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, EventArgs e) { this.fileInfoList = GetFileInfoList(); this.listBox.DataContext = this.fileInfoList; } #endregion #region 리스트 박스 선택 변경시 처리하기 - listBox_SelectionChanged(sender, e) /// <summary> /// 리스트 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox listBox = sender as ListBox; int? index = listBox?.SelectedIndex; if(index >= 0) { string filePath = listBox.SelectedItem.ToString(); if(!string.IsNullOrEmpty(filePath)) { Uri uri = new Uri(filePath); BitmapImage bitmapImage = new BitmapImage(uri); FileInfo fileInfo = new FileInfo(filePath); this.image.Source = bitmapImage; this.imageSizeTextBlock.Text = bitmapImage.PixelWidth + " X " + bitmapImage.PixelHeight; this.imageFormatTextBlock.Text = bitmapImage.Format.ToString(); this.fileSizeTextBlock.Text = ((fileInfo.Length + 512) / 1024) + "K"; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 파일 정보 리스트 구하기 - GetFileInfoList() /// <summary> /// 파일 정보 리스트 구하기 /// </summary> /// <returns>파일 정보 리스트</returns> private List<FileInfo> GetFileInfoList() { List<FileInfo> fileInfoList = new List<FileInfo>(); string currentDirectoryPath = Directory.GetCurrentDirectory(); string imageDirectoryPath = Path.Combine(currentDirectoryPath, "IMAGE"); string[] filePathArray = Directory.GetFiles(imageDirectoryPath, "*.jpg"); foreach(string filePath in filePathArray) { FileInfo fileInfo = new FileInfo(filePath); fileInfoList.Add(fileInfo); } return fileInfoList; } #endregion } } |