C# + XNA: Location of Sprite Folder and Sprites after Compile - c#

I am trying to program an automatic Resource Loader for my project and so it is going well. But I am stuck on the loading of the sprites. I used this for checking the file exists
if (File.Exists(contentManager.RootDirectory + "/Sprites/" + spriteName + ".png"))
However it never finds the file, even though the file exists in the Content of the project. I have checked the folders and found that the .pngs are replaced by .xnb files. So I tried changing the file extension and then was given an error in the Sprite Load function
return contentManager.Load<Texture2D>(assetName);
It only happens if I change it from being a typical image file (png, bmp, etc). Can someone explain why this is the problem and how I can get around this? I have had no experience in the xna file extensions and can not find the answers I need. What I want is to load a new image into the project providing it exists in the Content Directory after compile
While on the topic, will the Sounds and Music be effected by the Compiler?

When you use a content project, the resources are imported, processed and write to disk again as xnb files.
If you want to dynamically load resources you have to compile files on the fly through the content builder or avoid using the content project.
Microsoft provides examples like this to load content dynamically: http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2
if you want to distribute the application, the runtime xna is not enoigh, you need full xna framework and visual studio installed to work.
if you want the original files in the content project, you have to change their "copy to output folder" property to "copy always", and realize that the compiled results will be at "project folder\bin\debug\content"

Related

Could not load assets as non content files on Android?

I have included a SpriteFont asset and an mp3 asset in my MonoGame cross platform project, the strange thing is that the the game compiles and runs fine on the iOS simulator but whenever I try to build the game on Android I get warnings like this for the files in the bin directory and in the content directory
/Users/edward/Library/Mobile Documents/com~apple~CloudDocs/apps/Xamarin/Android/Radiant/Android/Content/lights.mp3:
Warning XA0101: #(Content) build action is not supported (XA0101) (Radiant.Droid)
And when I try and run the game, I get a
Could not load score asset as a non content file
I have tried many things such as changing the build actions of various files but nothing has worked.
This could be an issue of upper case and lower case file names. I've run into this issue before.
If it's not that, you can extract the Android *.apk file like a *.zip file to see what files are actually in there. This can seriously help on figuring out why files aren't being loaded with android.
To Extract
Create a copy of your *.apk file, so that you're not modifying the original.
Change the file extension too *.zip.
Now right click and extract like any other *.zip file.
You can then go in and explore the folder hierarchy to see what files are being included.
Check your Asset folder to see if the file you're looking for is there or not.
Hope this helps.
Are you using the monogame content builder or are you trying to load them in directly as .mp3 .png or whatever?
If you aren't using the content builder everything has to be .xnb on android.

how can i assign external images which loads in both runtime & design time with relative path to where the exe is built?

I want to create a guide application using C# which contains screen shots , texts and links to other pages.
I know that later on I have to update the guide application with its images.
The application including images is arround 2 MBs for each language. so I want to update it with the differences.
The solution i was thinking of is having a simple exe with PNG files next to it.
So I just need to include the exe and the only PNG files that have changed in my update patch.
The exe loads PNG files using PictureBox.ImageLocation attribute with relative path to where the exe is built. it runs ok. everything is fine but in Visual Studio's Designer it fails to load the images. It shows a cross sign on the Picturebox. The ErrorImage. I want to keep it relative because I use several PCs or maybe a team for the future.
Here is a sample project:
https://www.dropbox.com/s/o20xzc96cv422g9/WindowsFormsApplication3.rar
Runtime:
Designer:
Works ok at runtime, fails at designer.
how can i assign external images which loads in both runtime & design time with relative path to where the exe is built?
Is there a better way to create low size patches?
The problem is fixed by putting the images inside the resources of many Class Library Projects. So the project can be divided to so many parts, each part has its own data that can be updated later with lower sizes.

asp.net file upload blank image files

I have written some code for saving an image to a folder in asp.net. My problem is that the image in the folder is white and is not the same as images added manually to the folder.
I used a simple asp.net fileupload control to save the file to the correct path. But the images dont display on the page and this is how the file icons look in visual studio.
Anybody know why this is?
Try Right-clicking the images and select "include in project"
edit
If you want to do that programmatically you need to modify the project file programmatically; that's all there is to it. It's an XML file with nothing special about it. Note, however, that you have this under source control and you'll probably need to do more than just modifying the project file (ie adding the file to source control too)
Yes, it is. Because it is not included as part of the project files.
Try this:
There isn't anything else that is wrong. Only the files are not tracked by VS, so they won't be published. Your files are still completely accessible from your code.
In my opinion, files like say images added to your web app shouldn't be part of the project.
You need to include them in the project by right clicking them and click Include In Project.
Furthermore, if you want these files to included in the build you need to go to Properties of each file and set Build Action as Content.

Loading Content images from Blend from separate project

I have a VS2010 solution with 5 projects. Two of these projects are called:
MyResources
Application
The Application project contains all of the views and viewmodels for the application to run, and the Resources project contains all the images, resource dictionaries, etc for theming and skinning.
The way I have images set up in Resources, I have their build action set to Content, and Copy set to Copy Always. In my Application project, I reference these files using a pack URI. Example:
"pack://siteoforigin:,,,/Themes/DefaultTheme/BackgroundImage.png"
This works fine for me. The themes folder and all its subfolders are properly copied to the proper folder since I'm using siteoforigin://.
However, this breaks Blendability. When I load the project in Blend 4, I get errors.
If I use siteoforigin, Blend tries to load the images from the executing folder of blend.exe (In this case, C:\Program Files\Microsoft Expression\Blend 4 Beta\Themes\Default_Theme\BackgroundImage.png")
If I change the pack URI to the following:
pack://application:,,,/IQ.IQKiosk.Resources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "themes/default_theme/backgroundimage.png"
If I try:
/MyResources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\MyResources;component\Themes\DefaultTheme\BackgroundImage.png"
If I try:
/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\Themes\DefaultTheme\BackgroundImage.png".
So now I'm stuck. I'm not sure how to reference my images without having to embed them into the resource project, and I can't hard code the directory because my other coworkers have the project in different folders as well.
I tried to think of a way to get the proper URI to the proper locations of the files (relative to the .sln file would be awesome) or have the files get copied over to where blend compiles and executes the project. Alas, I am unable to figure out how.
Does anyone know the proper way to get these images to load?
I figured it out. Instead of using a pack URI, I just used
new Uri(Environment.CurrentDirectory + "\\Themes\\DefaultTheme\\BackgroundImage.png")
and it found the proper folder, since Evironment.CurrentDirectory gave me the proper bin folder where my themes were copied to.
Huzzah!

How to include non-compiled content alongside a C# program published via Visual Studio

I'm trying to publish an XNA game through the Visual Studio Publish tool. The game uses some compiled and some non-compiled content. Basically, I have a level loaded in via XML serialization and a short video. These two files are essentially streamed in, so they aren't compiled. The publish tool includes the compiled content fine, but any references to relative paths are broken because the CurrentDirectory for installed programs are set in the AppData folder.
I know that XNA can now compile XML without having to write custom content processors, but I don't particularly want to go back to rewrite that. I suppose I can if there's no other option, but that still doesn't remedy the video problem.
Is there any way to set up the publish tool so that I can do what I need to do? A setting or something? Or will I need to use a more fully-featured tool like NSIS?
Right click project, Add Existing Resource, browse and select the file you want to add. Then right click the file and click properties and change "Build Action" to content, and "Copy To Output Directory" to Copy if newer (or copy always if the need be). Then you can access it by using the relative path.
I use this for my XML and I can access my content using the following code:
XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");
Please note that my DefaultConfig.xml file is inside a "Resoruces" Directory which I created in Visual Studio(this is optional, but it helps me keep my project neat)

Categories