3

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.

Andrea
  • 320
  • 4
  • 10
  • 2 Suggestions. First, `@"Images\image2.jpg"`. Second, relative paths are usually relative to the current directory. Find out what the current directory is. `Environment.CurrentDirectory`. – agent-j Jun 18 '11 at 12:27
  • MonoMac runs on Mac OS X, so the path separator is indeed '/'. – Laurent Etiemble Jun 19 '11 at 20:22

3 Answers3

3

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

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
1

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

Utmost
  • 51
  • 1