tags:

views:

115

answers:

3

Hello,

I have an image in my xaml as so:

<Image Name="TotalFloors" Width="98" Source="../Images/FloorOne.png" Margin="0 0 0 10" VerticalAlignment="Bottom" />

This is loading fine, however everything i have tried to get this image change in code has resulted in nothing being displayed.

I tried a MessageBox.Show(TotalFloors.Source.ToString()); and it returned:

pack://application:,,,/MyClient;component/Images/FloorOne.png

Which then prompted me to use this code:

        private void GetFloorImg()
    {
        MessageBox.Show(TotalFloors.Source.ToString());
        BitmapImage floorImage = new BitmapImage();
        Uri uriSource;
        switch (App.selectedBuilding.Floors)
        {
            case 1:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorOne.png", UriKind.Absolute);
                break;
            case 2:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorTwo.png", UriKind.Absolute);
                break;
            case 3:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorThree.png", UriKind.Absolute);
                break;
            default :
                throw new NotImplementedException();
        }
        floorImage.UriSource = uriSource;
        TotalFloors.Source = floorImage;

    }

However this is not working either. The images are all set to resource in the folder. And a breakpoint shows that my code is being hit.

Any Ideas? TIA, Kohan.

+1  A: 

Try this:

private void GetFloorImg()
{
    string logoFileLocation = "pack://application:,,,/MyClient;component/Images/";

    switch (App.selectedBuilding.Floors)
    {
        case 1:
            logoFileLocation += "FloorOne.png";
            break;
        case 2:
            logoFileLocation += "FloorTwo.png";
            break;
        case 3:
            logoFileLocation += "FloorThree.png";
            break;
        default :
            throw new NotImplementedException();
    }

    TotalFloors.Source = BitmapFrame.Create(new Uri(logoFileLocation, UriKind.RelativeOrAbsolute));
}

PS: Build Action for Images should be "Resource" and after adding or modifying a resource, you must rebuild the solution. You know the first part (which you have written in the question) and I think you also know the second, but just in case.

Yogesh
+2  A: 

Just a little modification to your code :

private void GetFloorImg()
{
    MessageBox.Show(TotalFloors.Source.ToString());
    BitmapImage floorImage = new BitmapImage();
    Uri uriSource;
    switch (App.selectedBuilding.Floors)
    {
        case 1:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorOne.png", UriKind.Absolute);
            break;
        case 2:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorTwo.png", UriKind.Absolute);
            break;
        case 3:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorThree.png", UriKind.Absolute);
            break;
        default :
            throw new NotImplementedException();
    }
    floorImage.BeginInit();
    floorImage.UriSource = uriSource;
    floorImage.EndInit();
    TotalFloors.Source = floorImage;

}
Thomas Levesque
+1  A: 

This is the culprit:

floorImage.UriSource = uriSource;
TotalFloors.Source = floorImage;

instead use this:

TotalFloors.Source = new BitmapImage(uriSource);
TheVillageIdiot
This works great, im not quite sure what the difference between (BitmapImage floorImage = new BitmapImage(); floorImage.UriSource = uriSource;) and (new BitmapImage(uriSource);) They seem the same to me. Could you tell me what's different please?
Kohan
The UriSource property must be set between calls to BeginInit and EndInit (see my answer). This constructor does exactly that
Thomas Levesque
Great, thanks for clearing that up.
Kohan
Thanks @Thomas for information.
TheVillageIdiot