Click here to Skip to main content
15,886,634 members

How to select images from a folder and display it in a listview?

prashanth_mp asked:

Open original thread
Hi i'm trying to select images from a folder and then display those images in a listview. I tried to modify a sample code,when i run the sample the images are not getting displayed,i have posted the code here,can anybody tell me what i am doing wrong?
C#
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyImages = new ObservableCollection<MyImageClass>();
            OpenFileDialog ofd = new OpenFileDialog();
            
            ofd.Multiselect = true;
            ofd.Filter = "Image Files (*.jpg, *.bmp,*.png,*.gif)|*.jpg;*.bmp;*.png;*.gif|All Files|*.*";
            if (ofd.ShowDialog() == true)
            {
                string[] filePath = ofd.FileNames;
                int i=filePath.Length;
                string[] safeFilePath = ofd.SafeFileNames;
                foreach (string imgPath in filePath)
                {
                    foreach (string filename in safeFilePath)
                    {
                        MyImages.Add(new MyImageClass(filename, GetImageFromResourceString(imgPath)));
                    }
                }
            }
            
            //MyImages.Add(new MyImageClass("earth", GetImageFromResourceString("earth")));
            //MyImages.Add(new MyImageClass("mercury", GetImageFromResourceString("mercury")));
            //MyImages.Add(new MyImageClass("venus", GetImageFromResourceString("venus")));
        }
        public ObservableCollection<MyImageClass> MyImages { get; set; }

        private BitmapImage GetImageFromResourceString(string imageName)
        {
            BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.UriSource = new Uri(imageName);
                    image.EndInit();
            return image; 
        }


    }

    public class MyImageClass
    {
        public MyImageClass(string title, ImageSource image)
        {
            this.Title = title;
            this.Image = image;
        }

        public string Title { get; set; }

        public ImageSource Image { get; set; }
    }


Here is the xaml
XML
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="ListBox">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="MyImagesItemTemplate">
            <Grid Width="100" Height="100">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Image Grid.Row="0" Source="{Binding Path=Image}" />
                <Label Grid.Row="1" Content="{Binding Path=Title}" />
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid >
        <ListBox ItemsSource="{Binding Path=MyImages, ElementName=window1}" ItemTemplate="{StaticResource MyImagesItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
    </Grid>
</Window>
Tags: C#, .NET, WPF, ListView, Image, Binding

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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