Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:16:13+00:00 2026-06-17T06:16:13+00:00

I’m trying to make my game load compiled xnb textures outside the main game

  • 0

I’m trying to make my game load compiled xnb textures outside the main game class, as I usually do with my image files (which are not compiled to xnb format) and it doesn’t work.

Texture2D.FromStream returns “unexpected error”, and

using (var g = new Game1())
{
    Texture2D t2d = g.Content.Load<Texture2D>(file);
}

says that the file doesn’t exist, while File.Exists right next to it shows that it indeed exists.

I can’t add those xnb files to my project, because I won’t have them at compile time. How do I solve this?


More code:

public void LoadXNA(Microsoft.Xna.Framework.Content.ContentManager cmgr, string path)
{
    string xnaTexturesPath = path.Substring(0, path.LastIndexOf("\\")) + "\\textures xna";

    if (Directory.Exists(xnaTexturesPath))
    {
        var files = Directory.GetFiles(xnaTexturesPath, "*.xnb");

        foreach(var file in files)
        {
            string filename = Path.GetFileNameWithoutExtension(file);

            string ext = Path.GetExtension(file);
            string pathNoext = file.Replace(ext, "");

            var t2d = cmgr.Load < Texture2D > (pathNoext);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:16:14+00:00Added an answer on June 17, 2026 at 6:16 am

    First of all, you do not need an entire Game. All you need is a GraphicsDevice. Note that the textures are bound to the graphics device – so it must remain in existence while the textures do – and textures cannot be shared between multiple devices.

    To get a ContentManager without a Game class, have a look at the XNA WinForms Sample. In particular, it shows how to create a GraphicsDevice bound to a given control (which can be a Form – you can make it a hidden one if you don’t need to use it otherwise), how to make a service provider and put a IGraphicsDeviceService in it, and then how to create a ContentManager using that service provider.

    Alternately, have a look at this answer which provides some code which you can just use directly (it makes use of some classes in that same WinForms sample). It’s XNA 3.1, so it may require minor tweaks for 4.0, but should generally work.

    Texture2D.FromStream cannot load XNB files. XNB files need to be run through ContentManager to basically “unwrap” all the XNB stuff.

    Possibly the problem you are experiencing with ContentManager.Load is because you are including the file extension in your file name. It adds the .xnb for you, and will get confused if you add it as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.