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
Related
I am creating a desktop application in which user can add persons and their images. Currently, the images folder is in bin folder but when I publish the application and run on client pc then images folder gone missing.
OR
Can I add images in Project->Properties->Resources programmatically?
Currently when I tried to user this Images Folder in PMS Project then images added in the folder present in the bin folder. How I can add in this folder?
Currently, I tried to access this is
string path = System.IO.Directory.GetCurrentDirectory();
Bitmap imgImage = new Bitmap(pictureBox1.Image);
System.IO.File.Copy(ImageName, Path.Combine(appPath, CNIC + Path.GetFileName(ImageName)), true);
I also want to keep images folder in the installation folder.
But it goes in the bin folder. How I can achieve this?
If you plan to have a private folder where your app store some data then you should really use the standard specs. You should create a folder inside the Environment.ApplicationData defined in the SpecialFolder enum
You could have something like this in the startup code of your application
string commonFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string appFolder = Path.Combine(commonFolder, "PMS");
string imgFolder = Path.Combine(appFolder, "PoliceImages");
Directory.CreateDirectory(imgFolder);
Now those strings variables should be stored inside some kind of global configuration static class where you can retrieve them in any part of your application
If you want the folder be created automatically, you can add the basic images that would go into that folder and mark their Build Action and Copy to Output Directory properties appropriately in their properties window. This will make sure the folder gets created and also images will be copied in published copy.
Alternatively, If there are no images from development time, you can create the folder in client's installed location, using System.IO classes. Like -
System.IO.Directory.CreateDirectory("ss")
However, I would like to mention that its not a good practice to allow users to add images into a sub folder in bin unless when it is going to be a part of application itself after adding. If it is a content or data, try to keep it out of the folder where application binaries are installed. Probably,
Use an appropriate database storage.
C:\Users\[USERNAME]\AppData\Roaming
var directory = Environment.GetFolderPath(Environment.SpecialFolder.AppData);
OR
var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Or simply D:\Temp\[ApplicationName]
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 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));
I have a program that plays a sound but if I run it on a different computer it says that the file isn't found how do I attach the file onto the exe so when someone plays the exe it will still be able to play?
If you are wanting to embed your wave file into your program, go to your Project Properties --> Resources --> Select Audio as the type then select Add Resource and select your Audio File. This will add the Audio File to your Resources Directory. Once you have done this you can right click on the File in the Resources folder and select Properties and change the Build Action to Embedded Resource.
To access the file you would do something like this:
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.tada);
sp.Play();
When Packaging your project files create a folder inside your project folder to hold your content data (text - pictures - videos - sound .. etc) and then supply your code with the relative path of your target file rather than the absolute path .
for example : instead of
string path = "c:\Projects\ProjectFolder\FileName";
do it like that :
string currentDir = AppDomain.CurrentDomain.BaseDirectory;
string fullPath = currentDir + "ContentFolder/FileName.mp3(or whatever)";
Edit :
AppDomain.CurrentDomain.BaseDirectory
Will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client app, it will be the directory containing the main executable.
You can go the properties of the file, and under Copy to output Directory select, copy if newer
You can package the EXE with your c# program when you are ready to release it with an installer
If you cannot change the source code,I suggest
http://m.instructables.com/id/How-to-create-portable-app/?ALLSTEPS
It is about creating new exe archive containing old exe+new files you want to merge. When you call that new exe, it runs your old exe, but also contain your music files You can follow step4 and 5
Put your exe and music files into the same folder and compress them through that method
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.