Why can't I set the image source in a WPF application?

I am just learning WPF. I am using VS 2008. I added an Image control to my XAML and I added a GIF file to the project. If I use the XAML Designer properties panel to set the Source property of the image, the drop-down list for the Source property contains the following:

C: \ Sample Project; component / pinHorizontal.gif

There are several issues with this:

  • My project called "Sample Project" is not at the root of my drive.

  • Why is there a "component"?

If I select this value as indicated, I get the error "Property value is invalid" (yes, I'm not joking).

If I enter the XAML source and set the Source property manually, for example:

<Image Name="PinImage" Source="pinHorizontal.gif"/> 

The XAML constructor gives me this error:

"The file pinHorizontal.gif is not part of the project or its property" Assembly property "is not set to" Resource "."

Why is this task so difficult? How to assign an image source?

+4
source share
6 answers

Read this article - although be warned that this is not an especially easy read :-)

I suspect you want your image path to be pack://application:,,,/pinHorizontal.gif . if your image is set to BuildAction Resource , this will work fine.

+4
source

Right-click on the image file (pinHorizontal.gif) in SolutionExplorer, go to the properties, here is the 'Build Action' property, you need to set it to 'Resource'. Hope this helps!

+2
source

This post is "as well" ... not an answer to the original question.

I just thought I would document it if in the future someone else has a bad booger having this problem ... and googling that the error message finds this question ... so this is a good place for this.

I create new icons (actually bitmap images) and add them on the fly to the Visual Studio 2008 project (i.e. Add ~ Existing Item). The IDE reports errors file ${filename} is not part of the project or its 'Build Action' property is not set to 'Resource' on my XAML page ... but my project builds and works fine ... so what gives? ??

I tried everything I could think of to get a visual studio, to update her view of the file system, but to no avail.

Well, I just confirmed that restarting Visual Studio "updates" its file system / assembly contents and output cache, which causes these annoying non-IDE errors to disappear. Therefore, I add a batch of icons, mark them as Resources ~ Copy, if they are newer, build, and then restart the IDE, and all this is good.

Greetings to all. Whale.

+1
source

In Solution Explorer, select your project and right-click to rebuild the solution , and try adding the image again, it should work fine.

- or -

on the Assembly tab, select rebuild solution and try adding the image again.

0
source

You can use this code snippet in XAML to load an image from an absolute path without setting anything. Use DecodePixelWidth or DecodePixelHeight to save application memory.

 <Image> <Image.Source> <BitmapImage DecodePixelWidth="200" UriSource="C:\image.png" /> </Image.Source> </Image> 

Hope this helps!

0
source

I had the same problem. I just manually typed in the file location when the "Select Image" window appears, and it worked for me.

I re-opened the “Select Image” field after I got it to work, and noticed that file:/// now in front of the file location. I know almost nothing about Visual Basic (this is what I use), so it may be a good explanation for this, but I don't have one.

-1
source

Source: https://habr.com/ru/post/1304394/


All Articles