2

I am having trouble getting an image, as a resource, to show in a listBox element.

It looks like a lot of people are having trouble with it, it seems difficult to do, when to me it sounds like something that would be very common and easy to do.

I've got a file called 'home_icon.jpg' in a folder called 'images' within my resource, so: images/home_icon.jpg.

I've got a list box, for which the full code is shown below:

<Window x:Class="WpfApplication1.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>
        <ListBox Height="253" HorizontalAlignment="Left" Margin="10,46,0,0" Name="listBox1" VerticalAlignment="Top" Width="481">

            <ListBoxItem>

                <StackPanel Orientation="Horizontal" >
                    <Image Source="" Width="50" Opacity="50" />
                    <TextBlock Margin="0,5,0,0" Text="13 Scale Hall Lane" Width="425" />
                </StackPanel>

            </ListBoxItem>

        </ListBox>
        <Label Content="Welcome to the house manager" Height="28" HorizontalAlignment="Left" Margin="14,12,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>
</Window>

I've read a bit online, and I've learned to set the "Build action" for the image to "Resource.

Build action for home_icon.jpg

Here is the view of my solution explorer, showing the image in a folder: Solution explorer

What do I put in the Source="" attribute for the Image on the stackpanel for it to show my image?

I've tried looking at the following resources and things just aren't sticking:

http://msdn.microsoft.com/en-us/library/aa970069.aspx#The_Pack_URI_Scheme

http://channel9.msdn.com/forums/TechOff/416423-WPF--How-to-load-image-from-resource

WPF image resources

Community
  • 1
  • 1
Luke
  • 22,826
  • 31
  • 110
  • 193
  • What have you tried? did Source="images/home_icon.jpg" not work? If it didn't check if it is in the same directory, that could be an issue. In my case I use a resources folder, so I have to do : Source=../../Images/home_icon.jpg – MBen Oct 10 '11 at 12:52

1 Answers1

2
  1. Build Action should be Content .
  2. Copy To Output Directory should be Copy if newer;
  3. Then absolute or relative paths should work ... images/home_icon.jpg should work or pack://application:,,,/images/home_icon.png should work.
WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • The URI is working now - showing the image. But I'm still getting an error for some reason `Invalid URI: The Authority/Host could not be parsed.` – Luke Oct 10 '11 at 13:42