Jesse Cleary
Jesse Cleary

Reputation: 123

C# Relative Path to Image Will not Work

I've looked at about a dozen different solutions to get this to work and I just can't seem to execute it properly. I have a file cup.png that I placed in a subfolder called /Images/ located in my project folder under "/Visual Studio 2013\Projects\PointOfSale\PointOfSale\Images\cup.png".

The IDE acts like it can find the file and it shows up in the designer view and everything, and I can compile, but the minute I run it I get a XamlParseException error:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '7' and line position '14'.

What in the world am I missing here? I've tried all different forms of these to try and get this to work:

<ImageBrush ImageSource="pack://application:,,,/PointOfSale;component/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/Images/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/PointOfSale;component/Images/cup.png" Opacity="0.1"/>

Upvotes: 1

Views: 1383

Answers (1)

myermian
myermian

Reputation: 32505

You have a couple of options (your response to my comment did not answer this directly)

  1. You can include the images as a resource using the BuildAction of Resource.
  2. You can include the images as a piece of content using the BuildAction of Content.

If you choose the first option, then replacing the image would require the entire assembly/executable to be replaced. You can reference the image as "assembly;component/Resources/Images/cup.png" (Note that you need a forward slash at the start of the string).

If you choose the second option, then replacing the image would require you to merely replace the image file. You can reference the image as "/Resources/Images/darkaurora.png" (Note that you need a forward slash at the start of the string).

Upvotes: 2

Related Questions