I\'m starting with C# and XNA. In the \"Update\" method of the \"Game\" class I have this code:
t = Texture2D.FromFile( [...] ); //t is a \'Texture2D t;\'
>
As Andrew Russell says, loading your image 60 times per second is not what you want to do. However, I would add: Don't use Texture2D.FromFile
at all!
XNA provides a robust content pipeline for you -- use it!
MyGame.LoadContent
, load your image using myTexture = Content.Load(@"My/Image/Folder/MyImageAssetName")
Content.Load
will load the compiled XNB version of your image. You can also do things in the image properties in your project such as set a masking color (ex: all white pixels in your JPG will be transparent in your game), scale the image, and change the asset name. (The asset name defaults to the image file name, but you can change it in the properties, and the asset name is what you'll use for Content.Load
.)
Content.Load
also caches the things it loads, so that if for some reason you have to call Load
on the same asset multiple times, you're not multiplying the memory you use.
The XNA content pipeline can be used for many other things as well as images, including 3D models, sound, and even your own classes.