I\'ve looked around a lot and the only methods I\'ve found for creating a Texture2D from a Bitmap are:
using (MemoryStream s = new MemoryStream())
{
bmp.Sav
When I first read this question, I assumed it was SetData
performance that was the limit. However reading OP's comments in the top answer, he seems to be allocating a lot of large Texture2D's.
As an alternative, consider having a pool of Texture2D's, that you allocate as needed, return to the pool when no longer needed.
The first time each texture file is needed (or in a "pre-load" at start of your process, depending on where you want the delay), load each file into a byte[]
array. (Store those byte[]
arrays in an LRU Cache - unless you are sure you have enough memory to keep them all around all the time.) Then when you need one of those textures, grab one of the pool textures, (allocating a new one, if none of appropriate size is available), SetData from your byte array - viola, you have a texture.
[I've left out important details, such as the need for a texture to be associated with a specific device - but you can determine any needs from the parameters to the methods you are calling. The point I am making is to minimize calls to the Texture2D constructor, especially if you have a lot of large textures.]
If you get really fancy, and are dealing with many different size textures, you can also apply LRU Cache principles to the pool. Specifically, track the total number of bytes of "free" objects held in your pool. If that total exceeds some threshold you set (maybe combined with the total number of "free" objects), then on next request, throw away oldest free pool items (of the wrong size, or other wrong parameters), to stay below your allowed threshold of "wasted" cache space.
BTW, you might do fine simply tracking the threshold, and throwing away all free objects when threshold is exceeded. The downside is a momentary hiccup the next time you allocate a bunch of new textures - which you can ameliorate if you have information about what sizes you should keep around. If that isn't good enough, then you need LRU.