Click here to Skip to main content
15,868,049 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am loading images dynamically. But I am getting problem in loading the images

Here is the output. http://i57.tinypic.com/v7awye.jpg[^]

XAML code is...

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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="90"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Name="txtFolderPath" MinWidth="120" Margin="5" Text="E:\\QueleaTest" />
            <Button Grid.Column="1" Name="btnLoadFolderPath" Content="Load" MinWidth="80" Margin="5" Click="btnLoadFolderPath_Click"/>
        </Grid>
        <ListView Grid.Row="1" Margin="0,0.2,-0.4,-242.4" Name="lstView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Image Width="100" Height="100" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>


C# code is..

C#
List<Image> lstImages = new List<Image>();

        private void btnLoadFolderPath_Click(object sender, RoutedEventArgs e)
        {
            Image imgTemp;
            List<string> lstFileNames = new List<string>(System.IO.Directory.EnumerateFiles(@"E:\Quelea\images", "*.png"));
            foreach (string fileName in lstFileNames)
            {
                imgTemp = new Image();
                imgTemp.Source = new BitmapImage(new Uri(fileName));
                imgTemp.Height = imgTemp.Width = 100;
                lstImages.Add(imgTemp);
            }
            lstView.ItemsSource = lstImages;
        }


Images are there, but are not rendering..
Where m I doing wrong?

Please note, the images folder can be any on the filesystem and the image folder path is selected at runtime.
Posted
Updated 25-Sep-15 4:10am
v6
Comments
Sergey Alexandrovich Kryukov 25-Sep-15 10:21am    
You are not even trying to show them, you only create them.
—SA
Elder Scrolls 25-Sep-15 10:32am    
I also tried this

<ListView Name="QueleaGridView" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<itemspaneltemplate>
<wrappanel orientation="Horizontal">

</ListView.ItemsPanel>
<ListView.ItemTemplate>
<datatemplate>
<Image Width="100" Height="100" Source="{Binding ImagePath}">
</Image>

</ListView.ItemTemplate>
</ListView>

public class ImageClass
{
public string ImagePath;
}

List<bitmapimage> lstImages = new List<bitmapimage>();

BitmapImage imgTemp;
List<string> lstFileNames = new List<string>(System.IO.Directory.EnumerateFiles(@"E:\Quelea\images", "*.png"));
foreach (string fileName in lstFileNames)
{
imgTemp = new BitmapImage();
imgTemp.BeginInit();
imgTemp.UriSource = new Uri(fileName);
imgTemp.EndInit();
lstImages.Add(imgTemp);
}
ListView QueleaGridView = Quelea.UILibrary.UILib_MainWindow.FindChild<ListView>(Quelea.UILibrary.UILib_MainWindow.GetWindow(this), "QueleaGridView");
QueleaGridView.ItemsSource = lstImages;


But Same Result...
M confused. Stuck on it from so long. Did so many changes. Some right, some wrong.. Now how can I correct it?
Elder Scrolls 25-Sep-15 11:17am    
Can you tell, how to do this?

try following:

C#
imgTemp.Source = new BitmapImage(new Uri(fileName,UriKind.Absolute));


instead of
imgTemp.Source = new BitmapImage(new Uri(fileName));
 
Share this answer
 
You are not event trying to use the image objects you create.

They are not related to the images you prescribe via your XAML. If you really want to do it this way, you should give the names (by adding XML attributes Name to your XAML, so you could have reference to those objects generated via XAML, to use them in your code, through the named references). This way, you could use your *.PNG files as URLs via this property: https://msdn.microsoft.com/en-us/library/system.windows.controls.image.baseuri%28v=vs.110%29.aspx[^].

This is just the advice how could your quickly fix your application the way you envisioned it, but hardly and advice on how you develop it in a reasonable way. You generally do bad things. Form example, you hard-code image size (100), worse, you do it separately in XAML and code behind. How it possibly could be made supportable? And what if your URLs contain images of some different sizes? And so on… So, exact advice could be given if you explained what you really want to achieve with all that.

—SA
 
Share this answer
 
Comments
Elder Scrolls 25-Sep-15 10:43am    
Actually, I want to create a TileView. I am getting problems in loading a list of images in the TileView like above. The image folder should be selected at runtime. The system is displaying that there are some images to show, but not actually rendering them. Not sure, what to do.. Can you help?
Elder Scrolls 25-Sep-15 10:53am    
Can you correct the above code..
Sergey Alexandrovich Kryukov 25-Sep-15 20:55pm    
It's your turn now. I told you exactly what you can do if you want to follow your approach. If you are not even trying to use my advise, I doubt fixing your code would be an appropriate action.
—SA

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