Discussion:
Fastest method of creating Texture
(too old to reply)
Colmeister
2009-05-06 09:56:01 UTC
Permalink
Hi,

I'm writing an app using Managed DirectX in C# and have an image in the form
of an array of Color structs that I need to convert to a Texture in order to
draw using the Sprite class. What is the quickest way of doing this?

The most obvious way is creating a Bitmap and then using Texture.FromBitmap
but I don't know if this is the fastest. There are also many ways to create
the Bitmap, I've found to my surprise that the Bitmap's SetPixel method is
actually quicker than creating a Graphics object from the Bitmap and using
DrawLine/DrawRect.

Thanks in advance,

Colin
Wizfrog
2009-05-29 17:40:00 UTC
Permalink
The fastest way is to create an empty texture, lock the rectangle,
then copy the array directly into the locked buffer:

Texture tex = new Texture(device, width, height, 0, Usage.None,
Format.A8R8G8B8, Pool.Managed);
GraphicsStream stream = tex.LockRectangle(0, LockFlags.None);
stream.Write(dataArrayofColors);
tex.UnlockRectangle(0);

Loading...