XNA loading model from path dosen't work - c#

I have some problems with loading from path a model.
axis = Content.Load<Model>("XYZ");
I try to change it to :
axis = Content.Load<Model>("C:\\XYZ");
But it gives me this error :
Error loading "C:\XYZ". Cannot open file.
I also googled that thing, but I found nothing, looked at this question : How Do I Load A XNA Model From A File Path Instead Of From The Content (eg. Model.FromStream but it doesnt exist) , but it didn't help me.

In the properties of your content project, you specify a root folder in your project. When content is built, it is placed in your bin\content directory matching the hierarchy of your content project.
Content is local to your content project, not absolute on disk. Loading an unprocessed model from an absolute path will not get your a model inside of XNA like you expect.

Related

How do I properly serve images from the wwwroot to razor pages in a "Page" subfolder?

I have a JavaScript function that creates a fading slideshow. It uses images in the directory:
"wwwroot/images/backgrounds"
If I load a page that's directly inside the Pages folder, the images load properly. But if I load a page that's inside a subfolder, the browser tries to get the images from inside that folder instead of from the root folder.
So instead of
"localhost/images/backgrounds/bg1.jpg"
I get
"localhost/subfolder in 'Pages' folder/images/backgrounds/bg1.jpg"
I'm still new to this, so I'm not sure why the wwwroot folder isn't serving the images properly, but I set my default page to one in the same subfolder and it works perfectly, so I'm at a loss at to what's wrong.
Any ideas or solutions would be greatly appreciated at this point.
You're loading your images from a relative path - that is, the path that's exactly under the current directory. You want it to be based on an absolute path - that is, off of the same directory, no matter where the page is. To do that, use a slash in the beginning of your path: /wwwroot/images/backgrounds
This is actually by design - imagine you had sub-directories in your web application that needed their own images. You can then load an image, like images/pageBackground.png that's relative to each page the user is on - very convenient! Just remember that leading slash - which means start this path from
the virtual root of the application instead of the current sub-directory - and you'll get the right file!

DataTable's sorting arrows not appearing (getting 404 in browser)

I'm using DataTables on a C# project and the header's sorting arrows aren't appearing---in fact, in the console I'm getting a 404 error.
So I created a local /images folder (which is what the default DataTables png url was pointing to) but the 404 error is still coming back.
I tried tinkering around with the file paths under the min.js file but to no effect. Am I changing the path names correctly or is there something else that's causing the 404?
A few things to note:
I know very little about C#
I'm working on this with Visual Studio 2019 and I'm still trying to get the hang of the layout
Solution Explorer
/ Content
/ images
// sort_asc.png
// jquery.dataTables.min.css (same level as /images)
dataTables.min.css snippet
.sorting_asc{background-image:url("images/sort_asc.png")} // not working
.sorting_desc{background-image:url("../images/sort_desc.png")} // original (how the file came)
BundleConfig.cs
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/jquery.dataTables.min.css",
"~/Content/fontawesome-5.9.0.css"
));
Console:
GET http://localhost:xxxxx/images/sort_asc.png 404 (Not Found)
Try writing path in this way:
"~/Images/sort_desc.png"
I suggest you do some reading on how paths work in HTML and CSS.
Given the structure you have provided us
.sorting_asc{background-image:url("/Content/images/sort_asc.png")}
This is a root relative URL.
Also note from your BundleConfig you have bundles.Add(new StyleBundle("~/Content/css"). This will serve your CSS bundle from http://yoursite.com/Content/css/bundledCSS.css and all paths will need to be changed accordingly even if the physical files are in the content directory. This is another reason why root relative urls are a good idea, the images are then independent of the location of the CSS file.

MigraDoc image not found

I have a script lets say in folder 'root'. I have a folder inside root called 'Images', and inside images an image called 'logo.png'.
root
script.cs
Images
logo.png
I have the following code to create a section:
Document document = new Document();
Section section = document.AddSection();
And I have tried this:
section.AddImage("../../Images/logo.png");
and this:
section.AddImage("Images/logo.png");
to try and add an image, but both return 'image not found'.
I've already tried updating MigraDoc to v1.5 but it didn't seem to work.
The "script" gets compiled and you get an assembly that is executed. You are using relative paths and by default the image will be searched relative to the working directory of the process which most likely is relative to the folder with the assembly. With ASP.NET things can be even more complicated.
Tip: The DocumentRenderer class has a WorkingDirectory property. If you set it then images with relative paths will be searched relative to this working directory. Set it to "root" and the second path "Images/logo.png" should work.
Forgive me because I realize the age of this thread before providing an answer, but I too had the same issue that the accepted answer was unable to resolve.
In C# you are able to use HostingEnvironment.MapPath to reference relative paths. In your case, it'd look like:
section.AddImage(HostingEnvironment.MapPath("~/Images/logo.png"));

How to implement a text file in content (XNA)

I am building a game which loads its map from a text file.
While creating the parts that handle maps, I simply kept the text file in the content folder and fetched it by its Windows filepath. This won't work for proper deployment (or even running the game from different drives) because it requires that the filepath be exactly the same.
I looked around for a way to include the text file the same way I would a Texture2D, but I cannot find any class that allows me to use it. Some answers to other questions suggested that I just use the text file from my content folder? How would I do that? My program's name is IslandQuest (placeholder; it doesn't even involve an island) so would I place the text file in the IslandQuestContent folder generated by XNA Studio? How would I access it from there such that its filepath doesn't depend on the drive configuration of a computer?
Thanks for any help.
This may not be the best way to do this but just looked back at what I did in my first year at university with XNA,
I added my txt file to the contents folder. Then in the properties for the file (select it in the solution explorer and view properties window) there should be "Copy to Output Directory", make sure this is copy if newer.
Then its just a case of
string.Format("Content/{0}.txt", filename)
I do think this can be improved perhaps by the following but it is untested
Path.Combine(Content,filename +".txt");
In my case I was reading XML file files from a data folder in my main project.
So under my project in Solution explore I had this set up:
WindowsPhoneGame1
...
data/
content.xml
Game1.cs
Program.cs
etc...
Where properties for content.xml were Build Action: Content and Copy to Output Directory: Copy always
In the class that read the file I used TitleContainer.OpenStream Method which according to the docs:
Returns a stream to an existing file in the default title storage
location. .... The stream returned is read-only. The file to be read must already exist, or this method will fail.
My example code
//open stream
Stream stream = TitleContainer.OpenStream("data/content.xml");
//do something with it...
Create a "Content" folder in your main project.
Put the files that cannot be put in the Content project in there.
Be sure to set all your content Build actions to Content and Copy Always.
The Content folder from your main project and the content in the content project will end up in the same folder when built.
The file path would still be "Content/file.ext" or whatever.

How can i set image link without adding it to my project?

I have a webproject where I want to read the Content/Images/Funny/ for all my funny images :-) I would want to generate a list in my controller of image urls and push it to the view.
How do I access it using relative path from project root? Dont want to use Server.MapPath since it will reveal my whole path on the server.
I started a new mvc3 project and Its Content folder I want to map up. Any hints?
in a controller's action, you can use
var path = new UrlHelper(Request.RequestContext).Content("~/Content/Images/Funny/");

Categories