开发者

Finding the path of an image file inside a monomac project

开发者 https://www.devze.com 2023-03-13 16:57 出处:网络
I am new to mono and monomac and i got stuck with something that i hope should be really easy to fix.

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, 开发者_C百科but i need to use a relative one. How should i get the right relative path?

I'm using Monodevelop.

Thanks.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消