30

I don't know if I asked it right, but basically what happened is that I made a winform app which loads its image from the resource folder.

The problem is that when I build the project and get the exe and give it to a friend, he won't have that resource folder like I do, so he'll get an error saying missing file.

How can I somehow mix, or combine, or attach the image with my app?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
vexe
  • 5,433
  • 12
  • 52
  • 81
  • Do you need to update the image, or allow users to add images? Or are these static images that will not change? – Erik Funkenbusch Jul 18 '12 at 21:15
  • possible duplicate of [Load image from resources in C#](http://stackoverflow.com/questions/1192054/load-image-from-resources-in-c-sharp) – Erik Funkenbusch Jul 18 '12 at 21:18
  • @MystereMan I don't think it's a dup (or at least not of the one you mention). This question is about how to embed the image; the other one is how to load the image. – Beska Jul 18 '12 at 21:21
  • @MystereMan ThanX man, as you said, I'm not trying to load the image. About your Question, Yes, The user has the Ability to change the background image of the form. (There is a Theme ComboBox where the user selects a theme) – vexe Jul 18 '12 at 21:24
  • @Beska - True, so I guess I just answered his next obvious question ;) – Erik Funkenbusch Jul 18 '12 at 21:25
  • My point is, can the user muck about with the images? Can they include their own? If so, you can't really use an embedded resource (although you could write some logic that uses embedded resources if the user is not using their own). You might be better off writing an installer that will install your application (including adding it to the start menu) and copy any additional files that are needed. – Erik Funkenbusch Jul 18 '12 at 21:30
  • Sorry, I meant as Beska said .. :) – vexe Jul 18 '12 at 21:30

2 Answers2

35

App Properties Window

You need to add it to the project by navigating to the Properties Window and going to the resource tab and adding the image from there.

Alternatively, from the PictureBox Control you can import resource from your computer.

Option 2

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • 3
    You can also include the image in your project, and mark it's build type to be Embedded Resource. – Erik Funkenbusch Jul 18 '12 at 21:28
  • 1
    agreed, but when I've done that it wasn't visible in the Resources Mgr Area. – Glenn Ferrie Jul 18 '12 at 21:30
  • @MystereMan so after I mark it, if i wanna load it, should I load it from the resource file ? I mean, should I do this: string path = "imagePath\\image.jpg"; and then load it like: BackGround = Image.FromFile(path); – vexe Jul 18 '12 at 21:35
  • so if do this, I don't have to send my friend the resource file with the exe right ? – vexe Jul 18 '12 at 21:43
  • no its embedded. you should send your friend what is in the 'bin' folder – Glenn Ferrie Jul 19 '12 at 02:04
4

if you don't need to update it in the future, compile your program with the image property build action set to embedded resource. if you need to change it in the future compile with property build action set to content.

Riccardo
  • 1,490
  • 2
  • 12
  • 22
  • May I ask you where Can I reach that ? – vexe Jul 18 '12 at 21:28
  • just right click on the image file from the file explorer in visual studio and select properties, one of the properties is 'build action'. Change it to embedded resource or content. its default value is None. – Riccardo Jul 18 '12 at 21:33
  • Thank you so much :) but Can you tell me the difference between "Embedded Resource" and "Content" ? – vexe Jul 18 '12 at 21:39
  • @VeXe - Content does not embed the resource, Embedded Resource does. – Erik Funkenbusch Jul 18 '12 at 22:10
  • If you want to change image once you made the build you can't do it if you marked the image as Embedded resource. Instead if you marked it as Content you can change whenever you want – Riccardo Jul 19 '12 at 10:32