[Q] Change source of image from .cs file

ChrisNexus

Member
Dec 26, 2010
6
0
0
30
Lund
Hi. How would you go about to change the source of an image from the .cs file? I've tried this:

xaml.cs:
Code:
BitmapImage yesImage = new BitmapImage(new Uri("/Test;component/Images/yes.png"));            
img.Source = yesImage;
The xaml file has a control <image Name="img" /> residing in it.

Would binding in some kind of way solve this?

Sorry for the basic question, but I've searched and wasn't able to find anything that covers this.
 
Last edited:

otherworld

Senior Member
Apr 13, 2008
131
13
0
Leeds
Hi,

You're doing it in the right way. I assume you know you're using a URI referencing an image in an external assembly (i.e. a referenced dll separate from the project your .cs file is in)? (in your case called 'Test').

Is the image definitely in an external assembly?
Is that assembly definitely called 'Test'?
Is the image you are trying to load marked as Build Action 'Content' in Visual Studio?

Here's some code that loads an image from an external DLL called Shared:

MyImage.Source = new BitmapImage(new Uri("/Shared;component/Icons/Tick.png", UriKind.Relative));

Here's some similar code that loads it from the same assembly as the .cs file:

MyImage.Source = new BitmapImage(new Uri("/Icons/Tick.png", UriKind.Relative));

Hope that helps,

Ian
 
  • Like
Reactions: ChrisNexus

ChrisNexus

Member
Dec 26, 2010
6
0
0
30
Lund
That worked! I changed the images (that are local to the project) "Build Action" to "content" and added the UriKind.Relative to the URI and also removed the /Test;component/ from the address.

What i find odd though is that the images (that I added to a folder called Images in the project) had the Build Action set to Resource as default and when i added the source via the property box of Visual Studio to the image control in the XAML it inserted the "/Test;component/Images/yes.png" for me and the images were visible. When changing the Build Action to Content they were gone again.

Anyway, it works now. Thanks :)