I am working on a program that generates a PDF using a third party .dll. I am trying to add a header image to the PDF by sending in the string path of the jpg (Logo.jpg) in the project that is in a folder I created (Images), but it is not working using code like this..
PDFPage.AddHeaderImage("String path of the jpg in the project")
PDFPage.AddHeaderImage("~/Images/Logo.jpg))
There is a "could not find file" exception.
It works fine when I point to a file on my computer like this...
PDFpage.AddHeaderImage("C:/Source Code/Source/images/Logo.jpg");
But, I do not want to point to a file on my computer. I want to point to a the file in the Images folder in my project where I put Logo.jpg. I also put the image in Resources, but do not know what the string would be to access it. Either way would be fine.
the .AddHeaderImage is expecting a string path.
If you set Build Action = Content and Copy To Output Directory = Copy Always on the properties of the image file in Solution Explorer in Visual Studio, then the image file will be output to your bin folder along with the compiled application. You can then use a path relevant to the executable.
PDFPage.AddHeaderImage("Images/Logo.jpg));
Related
So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
I have many sets of images (PNG) that are placed in different subfolders inside the Resources folder on the Assets of the project. When working on the editor I'm able to load the images from the different subfolders without a problem, by just simply use the Resources.Load() command and providing the path to the specific image that I'm trying to load, such as:
firstLeftCC = Resources.Load("Case2/Left/CC/IMG-0004-00001", typeof(Texture2D)) as Texture2D;
In this example the image "IMG-0004-00001" is placed in the CC folder, and the CC folder is inside the Left folder, and the Left folder is inside the Case2 folder, and finally the Case2 folder is in the Resources Folder.
But after building the project, for a Windows application, when I run the .exe file it doesn't load any of those images. After some research, it seems that the problem is related with the presence of the subfolders inside the Resources, since the path given to the Resources.Load() function to load the image doesn't exist in the build.
I would like to know if anyone knows a solution for this problem, or if it is possible to load the images from a given folder instead of trying to load them from the Resources folder.
Thanks in advance for the help.
Optional method of loading your PNG file during run time.
You can leave the Case2 in the Asset Folder during development. After you build for Windows, go to folder the exe built file is. Let's assume it is called Game.exe. There will be a folder called Game_Data in the-same directory the Game.exe executable file is.
Copy the Case2 folder to the Game_Data folder.
It should now look like this .... Game_Data/Case2/Left/CC/IMG-0004-00001.PNG
Now with code below, you can easily read your png files with the code below:
Texture2D firstLeftCC = null;
byte[] imageBytes;
string imagePath = Application.dataPath + "/Case2/Left/CC/IMG-0004-00001.PNG";
if (File.Exists(imagePath))
{
Debug.Log("Exist");
imageBytes = File.ReadAllBytes(imagePath);
firstLeftCC = new Texture2D(10, 10);
firstLeftCC.LoadImage(imageBytes);
}
Now you can freely use firstLeftCC as a Texture2D
Note: Unity creates .meta file/folder for every file/folder in the Assets directory. When when you copy the directory,IMG-0004-00001.PNG will have another file called IMG-0004-00001.PNG.meta. You should either NOT place your Case2 folder in the Assets folder during development or you will have to delete these yourself.
Include using System.IO;
Alternatively to Resources.Load, you can load images from a given folder using the WWW class. To do this the URL must use the file:// protocol and contains the path to the files according to your OS.
http://docs.unity3d.com/ScriptReference/WWW.html
http://answers.unity3d.com/questions/517414/how-to-use-www-to-load-local-files.html
In my c# windows project i create a installer file.I add some image into program file folder. So i want to locate program file folder and get the image.
Anybody know how to locate that?
You can use Application.ExecutablePath property
Gets the path for the executable file that started the application,
including the executable name.
i do this like, just make a folder call it Resources and add the image files in it and retrieve it like this and finally add the Resources folder while making setup. I found this easy.
Image imageNormal = Image.FromFile("Resources\\button_Image.png");
controlName.Image = imageNormal;
In case if you are looking for this
C# - How to get Program Files (x86) on Windows 64 bit and
How do get the path of Program Files regardless of the architecture of the target machine
You can use Assembly.GetExecutingAssembly():
var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
It's very Easy , Try the following code AppDomain.CurrentDomain .BaseDirectory
You can try this code to get iamges
Image.FormFile("Imagename.jpeg" )
this code itself located the application direcotry for example if you write
Image.FromFile("Imagename.jpeg") then it will retrive images in bin folder,
I'm having problem loading images to my project.
I have the image in a folder named microassig this folder is on my desktop, this folder is to be sent online to my tutor, thats my line of code:
private Image imageOpen = Image.FromFile("\microassig\openOff.bmp");
I don't want to put the directory c:/ because is directory will be different from mine hence why i'm just using the ("\microassig\openOff.bmp");
The problem is that the image doesn't load.
Locate your project's
bin\debug folder
You can do this by right-clicking on your project solution and clicking Open in Windows Explorer.
Store/Save the file as
openOff.bmp
Then you can just do:
private Image imageOpen = Image.FromFile("openOff.bmp");
You should not be using the Image.FromFile. You need to add the image as a resource into the project. See this link: http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
The \ doesn't imply your desktop. You should use GetFolderPath for that (See C# Get Special Folder)
There are a few ways to go about this. You could add your image as a resource.
Or, more in the line of what you are already doing you could change your code to something like this:
private Image imageOpen = Image.FromFile("openOff.bmp");
Then move the openOff.bmp to your bin folder (where the exe is saved).
While you keep the openOff.bmp in the same folder as your executable it should find it.