I used to fill a Color[] using XNA's

Texture2D.GetData<T>(int, Rectangle?, T[], int, int)

method, but there doesn't seem to be an equivalent overload on SharpDX's Texture2D. Anyone know how I can get this same functionality with SharpDX?


How can I fill a Color[] from a SharpDX.Toolkit.Graphics.Texture2D given a Rectangle in SharpDX?


1 Answer 1


SharpDX is a DirectX wrapper, not an XNA one. XNA's GetData methods are higher-level abstractions on top of the underlying DirectX functionality.

If you're using the D3D9 interface, you probably want a variant of LockRectangle. If you're using D3D11 you probably want some variant of MapSubresource (or the similar method for D3D10). Note that in D3D9 the operation is a method of the texture, but in 10 and 11 it's a method of device context.

  • \$\begingroup\$ I suppose I should have rephrased my question. My question is basically how can I fill a Color array from a Texture2D given a Rectangle in SharpDX? This is DX11 \$\endgroup\$
    – test
    Oct 10, 2014 at 23:56
  • \$\begingroup\$ Once you have called "MapSubresource," the output DataStream will contain the texture data. You can read it directly from the DataStream or copy it to a raw color array using the methods of the DataStream object (such as Read). \$\endgroup\$
    – user1430
    Oct 11, 2014 at 0:35
  • \$\begingroup\$ Is it possible to do it on the GraphicsDevice object instead of having to cast the GraphicsDevice to a DeviceContext (which I'm told is not recommended)? \$\endgroup\$
    – test
    Oct 11, 2014 at 1:31
  • \$\begingroup\$ No, ID3D11Device does not have the method. A device and a context are not the same thing, you can't cast them around. You should already have a context around (at least the immediate context) since you can't generate rendering commands without it. \$\endgroup\$
    – user1430
    Oct 11, 2014 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .