Sunday, April 12, 2009

"The file 'x' is not part of the project or its 'Build Action' property is not set to 'Resource'"

When working on a WPF based application and using the Image control you may encounter the following error message when trying to enter the source file name for the image:
The file 'x' is not part of the project or its 'Build Action' property is not set to 'Resource'
It's not quite clear to me what causes this and it's not quite clear why Microsoft didn't fix it in Service Pack 1 for Visual Studio 2008 but here is a solution that may work for you:
  1. Add the file to your solution.
  2. Set its 'Build Action' to 'Resource' (in my case the drop-down also offers 'Embedded Resource' but that's not what you want)
  3. Select the image control.
  4. Set its Source property to the image file name. It should show up in the drop-down list. Next it may display the error message mentioned above. Then rename the image file to a short filename. Try setting the Source property of the Image control again. It should be fine now.
In case you want to try editing the XAML code then here is an example of what you may want set the source property to:
<Image ... source="Foo.Gui;component/pictures/myLogo.jpg" ... />
The assumptions for this example are
  • Your project is named 'Foo.Gui'
  • That project contains a folder named pictures
  • Your picture is in that folder and also included in the project. The name of the picture is 'myLogo.jpg'

15 comments:

Anonymous said...

That tip helped me a LOT. Thanks.

Joseph Maza said...

This tip of yours helped me out a whole bunch. For the longest time, I could not figure out what was going on. THANKS!

Anonymous said...

Thanks for that! After the past 3 hours to find out it is that VS does not handle long filenames in the Image element, arrggghh!, makes me more than a little annoyed at Microsoft.

Imran Aliyev said...

Thank you very much. It's works

Anonymous said...

Thanks, this was driving me crazy!

Dawid Kowalski said...

I believe that it should be source="/Foo.Gui;component/pictures/myLogo.jpg" instead of source="Foo.Gui;component/pictures/myLogo.jpg". Without / before the assembly name it's not working (well for me at least).

Anonymous said...

I've noticed that after I add an icon file (i.e. help.ico), I had to rebuild the solution in order for the file to be populated in the properties window for the Icon property.
Thanks much for the Tip!

Anonymous said...

Top tip. Thanks.

Wanton Wanderer said...

Thank you thank you ! It had been bugging me for ages.

Anonymous said...

Thanks Manni , actually after puting your code also I was getting same error ,then I renamed image file and it works for me.

Johnny Jason Wadhwa said...

thanks man.. this really helpful tip.. keep it up..

Unknown said...

what if I had to load images from diff folders... I am strugling....

Unknown said...

Very well pointed out in your post!!!
I found a very strange behavior if I add a image as an existing item in expression blend everythign works fine.... may be the roots are pointing to whatever you have posted above...
Cheers!!!

Anonymous said...

Awesome Thanks

Dinesh said...

You r a genius man...Was jus scratching all over to figure this stupid behavior out....u saved my day....
Dinesh.

Post a Comment

All comments, questions and other feedback is much appreciated. Thank you!