memory texture in C#

Started by
3 comments, last by unbird 14 years, 3 months ago
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
Visit galaxyroad.com which soon will have english contents too
Advertisement
TextureLoader.FromStream?

By the way, MDX is pretty much dead
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/
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

This topic is closed to new replies.

Advertisement