Xna: Load Texture2D from Embedded Resource
If you’re writing an app which uses Xna, you may need to load a texture from an embedded resource. Here’s how:
First embed the resource in your app. Do so by choosing Embedded Resource as the Build Action in the properties of the resource.
After that you can load the Texture2D using a stream handle to the embedded file.
Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream("AppNamespace.Folder.font.bmp");
return Texture2D.FromFile(graphicsDevice, stream);
GetCallingAssembly() can be exchanged with GetExecutingAssembly() if needed. The name of the resource must be fully qualified with the app’s namespace and folders. I usually keep my resources in a folder Resources so I would have: AppNamespace.Resources.font.bmp.
| Print article | This entry was posted by Zachary on July 3, 2010 at 12:46 pm, and is filed under C#, XNA. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

