Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi I need to get all images from the specific folder dynamically and display in matrix(gridview) in wpf using c#. I am new to c# and wpf please guide me to go through




Thanks in Advance

What I have tried:

I have tried doing something like this

string[] filesindirectory = Directory.GetFiles(@"C:\Users\user\Downloads\");

List<string> images = new List<string>(filesindirectory.Count());
foreach(string s in filesindirectory)
{
images.Add(String.Format("~/Images/{0}", System.IO.Path.GetFileName(s)));
}
datagrid.ItemsSource = images;
Posted
Updated 14-Oct-18 21:41pm
Comments
pradiprenushe 12-Oct-18 3:12am    
So what is problem here? You are not getting files or getting error.
[no name] 12-Oct-18 11:45am    
Start with "one" file and image.

I once wrote a WPF app that loaded 52 'gif' images of playing cards into a uniform grid. I’ve fished out some of the code and posted it here in the hope that it may assist you.


From the xaml.

C#
<Grid>
       <Grid.RowDefinitions>
           <RowDefinition Height="*" />
           <RowDefinition Height="Auto" />
       </Grid.RowDefinitions>
       <Grid HorizontalAlignment="Center"  >

           <ListView ItemsSource="{Binding CardCollection }" >
               <ListView.ItemTemplate>
                   <DataTemplate >
                       <Grid HorizontalAlignment="Center" >
                           <Image   Source="{Binding CardGraphic}" />
                       </Grid>
                   </DataTemplate>
               </ListView.ItemTemplate>
               <ListView.ItemsPanel >
                   <ItemsPanelTemplate>
                       <UniformGrid Columns="13" HorizontalAlignment="Center"  />
                   </ItemsPanelTemplate>
               </ListView.ItemsPanel>
           </ListView>

       </Grid>

From the viewModel.
C#
private IEnumerable<Card> LoadImagesFromFile()
       {
           string fullCardsPath = Path.GetFullPath(Constants.RelativeFilePath);
           var filenames = new List<String>(Directory.EnumerateFiles(fullCardsPath, Constants.AllImagesOfType));
           return filenames.Select(fileName => new BitmapImage(new Uri(fileName))).Select((cardGraphic, i) => new Card(i, cardGraphic)).ToList();
       }

 public ObservableCollection<Card> CardCollection
       {
           get
           {
               return cardCollection;
           }
           set
           {
               cardCollection = value;
               NotifyPropertyChanged();
           }
       }

The Card class has
C#
public int Index { get; private set; }

    public BitmapImage CardGraphic { get; private set; }

      public Card(int index, BitmapImage cardGraphic)
      {
          Index = index;
          CardGraphic = cardGraphic;
      }

In my case, I defined the constants as
C#
public static  class Constants
   {

      public const string RelativeFilePath = @"..\..\Images";
      public const string ImageType = ".gif";
      public const string AllImagesOfType = "*.gif";

   }

 
Share this answer
 
I would create a class that inherits from the wpf image control, and the constructor would accept the path/filename as a paramter to be used as the Source.

Then, I'd loop though the list of found image files, and create an instanc e of the new derived class, and add each instance to the parent control in the form.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900