Jump to content
  • Advertisement
Sign in to follow this  

memory texture in C#

This topic is 3268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi I'm using MDX & C# (VS 2008), I'll have an array of pixels loaded in memory (I've read an encrypted image file - ECW - using a library and then made an array), how can I create a texture using this array? thanks

Share this post

Link to post
Share on other sites
For C# development, you probably want to switch to SlimDX which is a very thin managed wrapper around DirectX 9, 10 and 11. The guys that made SlimDX are very active on this board and are very responsive to feature requests or bug reports... I can higly recommend this library...

check it out at http://slimdx.org/

Share this post

Link to post
Share on other sites
In cases where the texture does not have to be created very often, (it's sort of a hack but works reliably to) create a BMP file from the data and use LoadTextureFromFile.

Share this post

Link to post
Share on other sites
MDX actually provides some functions to read/write arrays from/to textures.

These are the steps:

1. Get a Texture with the right size and format. Here I assume a
8-bit per color channel with alpha, and I only generate one surface level.

int width = 256;
int height = 256;
Format format = Format.A8R8G8B8;
Texture texture = new Texture(device, width, height, 1, Usage.Dynamic, format, Pool.Default);

2. Lock the texture (otherwise you can't write/read), or more precisely: Lock
one level of the texture.

GraphicsStream stream = texture.LockRectangle(0, LockFlags.Discard);

3. Now you got a stream to write to. This one even handles arrays.

int[] array = new int[height * width];

// ... fill the array


4. Unlock again (so you can use the texture now for drawing) and explicitly
free the stream to save resources.


For debugging purpose, it might be handy to save the texture now to a file in an
appropriate format, to actually see whether you got the channels right:

TextureLoader.Save("texture.png", ImageFileFormat.Png, texture);

The tricky thing is to get your array right. You can even use two-dimensional
ones ( int[,] ) but keep in mind that it has to be first rows, then columns (i.e. int[height,width]). Sometimes, when using "unaligned" sizes, you might even be forced to resort to a variant of LockRectangle which returns the pitch
(the memory layout width of the texture is not the same as the width).

Hope that helps.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!