0

I'm having trouble getting the Source of my Image set in the code-behind. Here's my XAML:

        <StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
            <Image Loaded="imgPicture_Loaded_1" x:Name="imgPicture"   ImageOpened="ImgSelectedPicture_ImageOpened_1" Stretch="UniformToFill" Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,30,0,0"></Image>
        </StackPanel>

And the code-behind:

 private void imgPicture_Loaded_1(object sender, RoutedEventArgs e)
    {
        imgPict = (sender as Image);
        //ScrollViewer scroll = this.LayoutRoot.Children[2] as ScrollViewer;
        
        imgPict.Source = new BitmapImage(new Uri("/project;component/Images/avatar.png", UriKind.RelativeOrAbsolute));
        //bindPicture(imgPict);
        

    }

Can anyone see what I'm doing wrong?

2
  • 1
    Are you sure that the BuildAction of image you want to display is set to Resource? The way you are initializing the image Uri is suitable for embedded resources. And the best practice is to use Content as BuildAction for images and Uri initialization will look like new Uri("/Images/avatar.png", UriKind.Relative). Commented Jun 17, 2013 at 13:36
  • Why are you waiting to set the image source? What issue are you trying to work around or avoid? Commented Jun 17, 2013 at 16:19

1 Answer 1

2

First, what i don't understand is the image path "/project;component/Images/avatar.png" don't think the ";" sign makes the path valid. This should work for you:

<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center"                 VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
           <Image Loaded="imgPicture_Loaded" x:Name="imgPicture"  Stretch="UniformToFill"
             Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top"             
             Margin="0,30,0,0"></Image>
 </StackPanel>

then in the code behind

private void imgPicture_Loaded(object sender, RoutedEventArgs e)
 {
    imgPicture.Source = new BitmapImage(new 
                          Uri("/Images/StoreLogo.png",UriKind.Relative));                                        
 }

You can set the image's "Copy to Output Directory" property to "Copy always".

Sign up to request clarification or add additional context in comments.

1 Comment

The URI format that was being used originally IS valid and allows referencing resources in different assemblies. Unless you're doing something unusual after you've built your XAP file there is no value in setting the "Copy to Output Directory" property. It doesn't impact the XAP file. It's there as a hang over from other, older project types where you may want the entire contents of the build/bin directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.