1

In the solution I have a console app (MyApp) which executes a method from another project (MyProject). In MyProject I have a folder with images. How can refer to an image from the folder with e.g. Image.FromFile()?

Methods like Directory.GetCurrentDirectory() will return the path of the MyApp not MyProject Am I missing something obvious?

3
  • you can use relative paths. e.g. for a file named Image.png in the directory Folder\Subfolder\ (relative to the executable), you could do: Bitmap image = Image.FromFile(@"Folder\Subfolder\Image.png"); Commented Nov 1, 2018 at 16:47
  • How MyProject call MyApp.MyMethod();? Commented Nov 1, 2018 at 16:47
  • It's the other way around - MyApp calls a method from MyProject. MyProject is referenced in MyApp, there's a using statement and then the method is called. I hope I understood the question correctly but please let me know if I can provide more info Commented Nov 1, 2018 at 17:00

3 Answers 3

3

You can mark your image files as embedded resource. Right click on your image files > Properties, set Build Action to Embedded Resource.

At MyProject, create public method which return image, in this case the path signature should be manifest resource name..

public Image FindImageByPath(string path) // eg: MyProject.ImageFolder.MyImage.png
{
     using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
     return Image.FromStream(stream);
}

Another way is to store your images into resource file (.resx).

Sign up to request clarification or add additional context in comments.

Comments

1

You could simply reference that file path directly, i.e. if I had pictures outside of my project folder I could use

string fileName = "picture1";
string fileExtension = ".png";
var image1 = Image.FromFile($@"C:\Users\Me\Desktop\Pictures\{fileName}{fileExtension}")

Alternatively, "..\" moves up a folder in the current directory and "\\" refers to a global directory

Remember to use the string literal '@' unless you want to escape every '\' in the file path. Use '$' for string interpolation If you use both '$' AND '@', you MUST use them in the order '$@'

6 Comments

I would like to have a relative path to the file in the project folder
in that case, it just depends how far outside of the current directory your desired folder lives. If it is one folder outside add "..\" to the end of the active directory and then the folder and image path. If it's 2 folders outside use "..\..\" etc...
The file is actually in the same folder (for the sake of simplicity) as the Service.cs which tries to reference the file but calling something like Image.FromFile("image.jpg") doesn't find the image. It finds the image though if I put there an absolute path in C drive
Try using something like Image.FromFile($"{Directory.GetCurrentDirectory()}image.jpg");
As I've mentioned in my question unfortunately Directory.GetCurrentDirectory() returns an absolute path to MyApp and not MyProject where the image is located
|
1

If the files are included in the project, another option is to go to the Properties tab and mark the Build Action as "Content" and the Copy to Output Directory as either "Copy always" or "Copy if newer." This way the files will appear in a path relative to your assembly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.