Michael Yochpaz
Michael Yochpaz

Reputation: 196

How do I store a resource path?

The program I'm making is pretty hard to explain since it's for a very specific personal use so I'll use an example to make it easy to understand:

I have a class in the project named Person that stores an image and a name of a person. All images are stored in the project resources.

How do I save the resource path/name so that I can reuse it on the main program (not the Person class) ?

For exemple if I create a new Person object in the main program:

Person p = new Person("Michael", Project.Properties.Resources.image);

As what type of variable do I save the path in the Person class?

public Person(string name, ??? image)

Note that I will need to reuse this image later,

for exemple:

this.imageBox.Image = p.image;

I tried using the Image and Bitmap objects but it just changed the imageBox to be blank (I think it sets the imageBox.Image to null. Also, I'm pretty sure that using Bitmap will copy the Image's data and use more memorey for no reason)

I also tried using Image.FromFile and inserting the path as a string but it didn't work.

Upvotes: 3

Views: 150

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157108

You should not be messing around with paths if you mean to use resources. They are embedded into the assembly and can be reused from there.

The easiest way to access them is by using the generated resources class, Project.Properties.Resources.Picture. The type of the variable will be an Image.

public Person(string name, Image picture)

If you want to, you can even access the resource by extracting it by hand from the assembly, but that seems to much for this case as far as I can tell.

Upvotes: 3

Related Questions