SIGN UP MEMBER LOGIN:    
ARTICLE

Get Icon From FileName in WPF

Posted by Diptimaya Patra Articles | WPF with C# January 17, 2010
In this article we will see how we can convert a filename to Icon.
Reader Level:
Download Files:
 

Introduction

In this article we will see how we can convert a filename to Icon.

Creating WPF Project

Fire up Visual Studio 2008 and create a new WPF Project. Name it as GetIconSample.

FileNameWPF1.gif

Here is the idea what we are going to do for our application; we would browse for adding files to our ListBox. ListBox would display the File Icon and File Name.

The following image what we discussed above.

FileNameWPF2.gif

First of all we would write a class that would convert FileName to Icon. To achieve this we need to refer the System.Drawing Namespace.

FileNameWPF3.gif

The following code is the FileToImageIconConverter

#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

Now we will create a simple class that would contain the Properties as FileName, FileIcon.

#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

The following XAML describes our overall design for ListBox and other controls.

<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>

Now we would add code under Button Click of our Browse button.

public partial class Window1 : Window
    {
        ObservableCollection<MyFiles> myFilesList = new ObservableCollection<MyFiles>();

        public Window1()
        {
            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;

                myFilesList.Add(new MyFiles { FileName = ofd.SafeFileName, FileIcon = imgSource });
            }

            lbFiles.ItemsSource = myFilesList;
        }
        #endregion
    }

That's it. Run the application to see what we have achieved!

FileNameWPF4.gif

Hope this article helps.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Suman.

Posted by Diptimaya Patra Jan 28, 2010

The below section is missing in the above screen shots & code. This is found in the Download Zip.

#region MyFiles
    public class MyFiles
    {
        public string FileName { get; set; }
        public ImageSource FileIcon { get; set; }
    }
    #endregion


Posted by Suman Jan 25, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor