Sign in to follow this  
vcGamer

memory texture in C#

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
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

stream.Write(array);



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


texture.UnlockRectangle(0);
stream.Dispose();




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.

unbird

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this