.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Get Icon From FileName In WPF

Posted By:Diptimaya Patra       Posted Date: March 20, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

Get Icon From FileName In WPF. In this article we will see how we can convert a filename to Icon.
 

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.


    
        
            
        
        
    
    
        

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

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

        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.


 Subscribe to Articles

     

Further Readings:

Responses
Author: kregel         Company URL: http://www.dotnetspark.com
Posted Date: January 10, 2012

How can I get this to work to return the 16x16 small size icons for file extensions?


Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend