How to constantly get same directory path from inner directory - c#

I am using c#.
I have a project running at path:
D:\USER\Desktop\Prgramming\C++ 2020-2021\project\projectGraphics\projectGraphics\bin\Debug
The Project directory looks like this:
I want to get constantly (in any computer) from an .exe file in Debug to an image in Resources efficiently. How can I do that?

Do you use WinForms or WPF?
Add the image as a "resource" in your project.
Afterwards you can use
Bitmap bmp = new Bitmap(Properties.Resources.myImageFile);
to access the image.

Related

Errors after changing tool strip button image

Once i add this line to the code:
this.tsbAdd.Image = Bitmap.FromFile(#"..\..\Resources\add.bmp");
I'm unable to open editor of that form.
Screenshot of designer
I can compile app and images work as they should.
Expected results - new image is displayed without breaking designer.
Real results - new image breaks designer.
Once i build it into .exe it doesn't open. Without images it works flawlessly.
Nope it won't. The picture will be built, but referencing it by this path won't be working.
The resource file WOULD be built into your exe, but not at "....\Resources\add.bmp". This path only exists in your IDE configuration position, when your program is at "bin\Debug", you understand me?
Imagine you put your exe into C:\, then where is "....\Resources"? You cannot refer to a image in this way.
You should add resources in the project panel (I believe you have done that), and the way you get this file is via ResourceManager, not using this path. Like this:
ResourceManager rm = Resources.ResourceManager;
this.tsbAdd.Image = (Image) rm.GetObject("add");
The resourcemanager will pull the resource bitmap out from your built exe. Just using that path won't work. As the designer is not ran in \bin\Debug, no wonder it is broken too because it cannot find your file using that path.

Relative Path when creating Bitmap

I am trying to create a Bitmap using:
bitmap = new Bitmap(#"Movies\View\Images\missing_person.bmp");
However, I am receiving a System.ArgumentException error.
The file I'm calling the above code from is located in:
MyProj\DisplaySideBarCommand.cs
The images are in:
MyProj\Movies\View\Images\missing_person.bmp
I also tried using:
bitmap = new Bitmap(#"..\Movies\View\Images\missing_person.bmp");
but received the same error.
It is going to look for the files relative to the executing assembly. When you build your project it is probably output to a directory like bin\debug or bin\release. You could build your relative path to backtrack from there, or you could copy the files to the output directory.
If you set the build action to Content on the files, they will be copied to the output directory (including sub folders) on build and then you should be able to build the correct relative path from there.
If you are using the default settings, the debug binaries would be in ProjectDirectory\bin\Debug\ - therefore, you might want to try #"..\..\Movies\View\Images\missing_person.bmp"
This is because the images are located in your project folder, not your output folder.
string projectFolder = "..\\..\\"; // Goes back to the project folder
Once you got the projec path simply use it like this:
bitmap = new Bitmap(projectFolder + #"Movies\View\Images\missing_person.bmp");
I would suggest you to move your files to the output folder rather than storing them in your project. Since normally you only distribute the output folder and not your whole project (Unless it's open source of course.)

Contents of bin folder after publishing .NET project to an MSI file - where will the image go?

I am working on a WinForms application which uses a flatbed scanner (in conjunction with PixTools library).
Currently when running in debug mode (unpublished), when the application code scans the image, it stores the image file .jpg in the bin folder of the project (C:...\Source\winapp\bin). My code then has to manually move this file using File.Move() to the directory where I want it to end up.
However I am thinking that when I publish the application to install on another machine, this bin folder won't exist. I am wondering where the application is likely to store the .jpg from the scanner in this case so I can write the code in advance. For instance, will the image appear somewhere in the installation folder which is selected when installing application the first time round?
Thanks for any advice/help
C
You can Create/Store your image file(.jpg) into your Debug/Release folder which is the path from where your Application exe runs.
The following code gives you the path from where your Application is running:
String ApplicationFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
The Above code returns the path similar to
SolutionFolder/ProjectFolder/bin/Debug(or)Release
So you can target your code to retrieve the image from the above location as it is avaialable even after deplyoing/Installing the Application on client PC.

How to build a stand alone exe with resources?

I have a WPF app with image resources. When I build the program, there is a directory named "resources" in the bin folder containing my images. If I run the program without the folder, it crashes. How can I embed the resources in the app so it can run stand alone?
You have to set the build action of the image files to Resource, like shows here. The Uri for an image file named Image.png would then be this:
pack://application:,,,/Resources/Image.png
See also the MSDN page Pack URIs in WPF.

Where do you store files(such as images) on Windows Mobile 6 phones?

I want to have a couple images in my phone application. However I don't know where to store them. Like I add them to my VS project but of course the image path is the path on my harddrive.
So is like special folder in windows mobile where I store it. Like if I transfer my application to the phone I am going to have to stick this image somewhere. It also has to be a place where it can't be deleted by the user.
Thanks
Either You add all images as resource and Build it,
or keep separately in programme files and reffer all images from there, ususally users doesnt delete content from programe files.
here is the steps to add image as resource,
1) Create a new C# Project
2) Go to project properties
3) Go to resource tab
4)Click on Dropdown button of Add Resource
5)Select Add existing File
6)Choose the image from Your system.
this leads to addition of images to your project resources.
Then in order to use the image just follow,The following syntax
YourProjectNameSpace.Properties.Resources.ImageFileName
here is the sample code of displaying the image taken from the Project resource,
Bitmap MyBitmap = new Bitmap(Delete1.Properties.Resources.Greenish_Logon_Vista_by_ralph1975);
pictureBox1.Image = MyBitmap;
i hope this helps..
I suggest that keep all images in sub-directory (i.e. images) under Application installed folder
e.g.: MyApp\Images
When you debug the project, images will not copy into Debug or Release folder, you have to copy paste them manually (Or as Matt mentioned, Select an image file from solution explorer and in Properties, Select "Copy To Output Directory" as "Copy always" or "Copy if newer". It will also keep the directory structure) and then copy whole Debug or Release folder into the device.
You can specify path of images in your application like:
// Get full application path
String full_path = System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase;
if (full_path.LastIndexOf("\\") != -1)
{
// Take away application name from full path and return current directory name
String directory_path = full_path.Substring(0, full_path.LastIndexOf("\\") + 1);
// Specify images directory
String directoryImages = directory_path + "images";
}
If you want to create an installer, then add folder and images into the installer (Or Right click on "Application Folder" under File System of CAB Project, Select "Add>Project" Output and choose "Content Files" and Press OK). When you will install application using .CAB file, it will install files and keep same directory structure of file (which are marked as "Copy always" or "Copy if newer").

Categories