patrick
patrick

Reputation: 16979

Add image files to a Visual Studio 2010 project

I need to load some image files in my application, and I want to just build them into the application instead of having the user point to their path. So I think I need a resource file. But I'm having a little trouble figuring out how to create a resource file and add it to my application.

Upvotes: 1

Views: 10760

Answers (2)

Nobody
Nobody

Reputation: 4841

In Windows Forms you can do this:

  • Right click on the project in Solution Explorer
  • Click on properties
  • Click the Resources tab on the left
  • Click the middle link that will come up

Then all you do is go to properties → double click Resorces.resx → and go to Add ResourceAdd Existing File.

Upvotes: 1

driis
driis

Reputation: 164341

You can add the file to your project by choosing Add Existing Item from the project menu. Then, in properties for the file (hit F4), choose that Build Action should be "Embedded Resource". Then your file will be embedded in the assembly.

You can get to the file by using Assembly.GetManifestResourceStream:

Type t = typeof(SomeType);
Stream embeddedFileStream = t.Assembly.GetManifestResourceStream(t, "yourfilename.jpg")

The "SomeType" should be in the same assembly as the embedded resource, and you will need to specify the name of the file relative to the namespace location of the type.

Upvotes: 3

Related Questions