Get Icon From FileName in WPF
WPF 에서 파일 이름으로부터 아이콘을 얻어오는 방법이다. 우선 테스트할 WPF 데모 프로젝트를 만든다. 프로젝트 이름은 GetIconSample 이라고 했다.
테스트할 프로그램은 ListBox 를 갖는다. ListBox 에 파일을 추가하기 위하여 파일을 탐색하기 위한 Brows 버튼을 갖는다. ListBox 는 추가된 파일의 아이콘과 이름을 출력하게된다. 데모 프로그램의 외관은 아래와 같다.

우선 첫 번째로 파일 이름을 아이콘으로 변경하는 클래스를 작성한다. 이 클래스를 작성하기 위하여 System.Drawing 네임스페이스를 참조 추가 시키자.

클래스의 코드는 다음과 같다.
#region FileToImageIconConverter
public class FileToImageIconConverter {
private string filePath;
private System.Windows.Media.ImageSource icon;
public string FilePath { get { return filePath; } }
public System.Windows.Media.ImageSource Icon
{
get {
if (icon == null && System.IO.File.Exists(FilePath))
{
using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath))
{
icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
sysicon.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
}
return icon;
}
}
public FileToImageIconConverter(string filePath)
{
this.filePath = filePath;
}
} #endregion
xaml 코드는 다음과 같다.
<Window x:Class="GetIconSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="337" Width="519"> <Window.Resources> <ItemsPanelTemplate x:Key="ItemsPanelTemplate1"> <WrapPanel IsItemsHost="True"/> </ItemsPanelTemplate> <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Padding" Value="2,0,0,0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Orientation="Vertical"> <Image x:Name="img" Source="{Binding FileIcon}" Height="32" Width="32"/> <TextBlock VerticalAlignment="Center" Width="75" TextWrapping="Wrap" Text="{Binding FileName}"/> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button x:Name="btnBrowse" Click="btnBrowse_Click" Content="Browse" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="119"/> <ListBox x:Name="lbFiles"
ItemContainerStyle="{DynamicResource ListBoxItemStyle}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Margin="12,51,12,12" ScrollViewer.VerticalScrollBarVisibility="Visible" /> </Grid>
{
get {
if (icon == null && System.IO.File.Exists(FilePath))
{
using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath))
{
icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
sysicon.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
}
return icon;
}
}
public FileToImageIconConverter(string filePath)
{
this.filePath = filePath;
}
} #endregion
xaml 코드는 다음과 같다.
<Window x:Class="GetIconSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="337" Width="519"> <Window.Resources> <ItemsPanelTemplate x:Key="ItemsPanelTemplate1"> <WrapPanel IsItemsHost="True"/> </ItemsPanelTemplate> <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Padding" Value="2,0,0,0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Orientation="Vertical"> <Image x:Name="img" Source="{Binding FileIcon}" Height="32" Width="32"/> <TextBlock VerticalAlignment="Center" Width="75" TextWrapping="Wrap" Text="{Binding FileName}"/> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button x:Name="btnBrowse" Click="btnBrowse_Click" Content="Browse" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="119"/> <ListBox x:Name="lbFiles"
ItemContainerStyle="{DynamicResource ListBoxItemStyle}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Margin="12,51,12,12" ScrollViewer.VerticalScrollBarVisibility="Visible" /> </Grid>
</Window>
버튼 클릭 이벤트를 다음과 같이 구현해 주었다.
public partial class Window1 : Window {
ObservableCollection<MyFiles> myFilesList = new ObservableCollection<MyFiles>();
public Window1()
{
InitializeComponent();
}
{
InitializeComponent();
}
#region Button-Click-btnBrowse
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "All files (*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filePath = ofd.FileName;
FileToImageIconConverter some = new FileToImageIconConverter(filePath);
ImageSource imgSource = some.Icon;
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "All files (*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filePath = ofd.FileName;
FileToImageIconConverter some = new FileToImageIconConverter(filePath);
ImageSource imgSource = some.Icon;
myFilesList.Add(new MyFiles { FileName = ofd.SafeFileName, FileIcon = imgSource });
}
}
lbFiles.ItemsSource = myFilesList;
} #endregion }
} #endregion }
댓글 없음:
댓글 쓰기