Andrea
Andrea

Reputation: 320

Finding the path of an image file inside a monomac project

I am new to mono and monomac and i got stuck with something that i hope should be really easy to fix.

In my application i want to change some images at runtime, so i created an "Images" folder with my pics inside the root of the project. But when i try to change the image in my code the image can't be found, so i think i'm writing the wrong path.

I try to get the image like this:

NSImage image = new NSImage("Images/image2.jpg"); // I tried with different paths but nothing changed...
pic1.Image = image;

If i use the full path to the image, everything work fine, but i need to use a relative one. How should i get the right relative path?

I'm using Monodevelop.

Thanks.

Upvotes: 3

Views: 1795

Answers (3)

Utmost
Utmost

Reputation: 51

You also use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) to get resources path.

Upvotes: 1

Uwe Keim
Uwe Keim

Reputation: 40736

In my own project I looked up the MonoMac samples on Github and I found one that also loads images at runtime.

They simply do a call to:

var myImage = NSImage.ImageNamed ("some.png");

I've placed my images in my project inside a sub folder "/Images" and this function still successfully loads my images.

So maybe this could be a solution to your issue, too.

Upvotes: 1

Laurent Etiemble
Laurent Etiemble

Reputation: 27889

You can use the NSBundle.PathForResourceAbsolute method to locate the image, then load it.

Upvotes: 3

Related Questions