简体   繁体   中英

cannot open imagesource with relative with relative path

I need some help with imagebrush it only works with absolute directories for example

<ImageBrush x:Name="bgimage" ImageSource="C:\Users\user\Desktop\MediaSampleWPF\MediaSampleWPF\obj\Debug\image.png" Stretch="Fill"/>

works fine but this

<ImageBrush x:Name="bgimage" ImageSource="image.png" Stretch="Fill"/>

gives me this error - if I replace image.png with notexist.png it gives me an error before compilation, but if I gut image.png it does not, meaning that the image actually exists

IOException: Cannot locate resource 'image.png'

the image is in every single directory of the solution, like, literally, i opened every folder in the project solution and i put image.png, but it still say that the image does not exist can someone help me?

I am guessing you are trying to do this in XAML? If so, have you tried adding the image to your project as a resource? My example is using an image on a button, but it will work the same for a brush. In the project, or in a project subfolder "Add Existing Item" via right-click with the mouse or from the Project menu.

Then you can use a relative path to the resource in the project.

XAML 片段

在此处输入图像描述

In the.csproj file it ends up as an XML entry that looks like this:

<ItemGroup>
  <Resource Include="Images\IO-64-black.png" />
</ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM