Forum > Having trouble loading a bitmap in C# 2008 using XNA 3.0

Sorry about the long title. Quite simply i'm having trouble loaiding a bitmap. The main reason could be because i'm following tutorials designed for XNA 2.0 and C# 2005, but i'm trying.

The problem is when it comes to runtime it says that the object wasn't found that i'm loading. I added the bitmap to my project by;

Project > Add Existing Item > Browsing > Added to project.

Then in the Game1.cs file I added a Texture2D command and labelled it spriteBall.

The part of the code where the error is spotted is when its loading the previously added graphic file.

spriteBall = Content.Load<Texture2D>("ball");

although I added the graphic using my above method it acts like it isn't there.

If i'm doing anything wrong could you please guide me onto the right path. As soon as my sprite is loaded onto the screen, I can then start defining my variables and moving the sprite around onscreen, I'll be buying your book soon Rob, so don't worry !

Thanks in advance.

August 19, 2008 | Unregistered CommenterJDog

I can think of two scenarios that might be causing your problem. Both are easy to fix, so don't worry about it.

1. When you add graphics files to your project, they should go into the "Content" folder. If you look in the constructor for Game1, you will notice the "Content" folder is assigned a root directory by this line:

Content.RootDirectory = "Content";

2. If you did put it in the correct folder, the other problem could be a wrong asset name, or you did not change the property for the asset to copy to the output directory. The asset name can be seen in the properties window which should be directly below your solution explorer. If not you can hit F4 to open the properties window. Make sure the asset name is the same in your file and in the properties window. Also in the properties window is the "Copy to Output Directory" option. Unless you manually move the file yourself, you should set this property to either "Copy always" or "Copy if newer".

Hope that helps!

August 21, 2008 | Unregistered CommenterShawn

Sorry you are having problems. Shawn has suggested all the things that make most sense. The only other thing I would add is to make sure that you are using a graphics file type that XNA is happy with. In other words gif, jpg or png. If you download the sample files for my book you should find some programs that actually load and position images (take a look at the samples for Chapter 4).

I've used exactly the same bitmap loading code in XNA 2.0 and 3.0 and it works fine. There were some changes to version 2.0, where the content directory was added, but other than that I can't seen anything wrong. If you want to drop your project into the drop box for the silly surgery I'd be quite happy to take a look.

August 21, 2008 | Registered CommenterRob

Thanks for the corresponadance so far. I think I messed up by not adding the graphics file in the content folder. When I added it, it automatically created an "obj" folder, which I assumed was correct. Also whenever I move the graphic into the content folder it creates another "obj" sub folder. So in my code how would I refer to it ?

spriteBall = Content.Load<Texture2D>("Content\\ball");

The above at a guess. Thanks for your help so far. I can see that picking up C# may be harder than i'd thought. I have fully read your pink book aswell Rob, a good read.

Thanks Shawn also !

August 21, 2008 | Unregistered CommenterJDog