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:

  1. Anonymous10:53 AM

    That tip helped me a LOT. Thanks.

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

    ReplyDelete
  3. Anonymous6:39 AM

    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.

    ReplyDelete
  4. Imran Aliyev6:58 PM

    Thank you very much. It's works

    ReplyDelete
  5. Anonymous5:24 AM

    Thanks, this was driving me crazy!

    ReplyDelete
  6. 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).

    ReplyDelete
  7. Anonymous1:39 PM

    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!

    ReplyDelete
  8. Anonymous10:11 AM

    Top tip. Thanks.

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

    ReplyDelete
  10. Anonymous6:19 PM

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

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

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

    ReplyDelete
  13. 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!!!

    ReplyDelete
  14. Anonymous5:02 AM

    Awesome Thanks

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

    ReplyDelete

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