Reputation: 311
this is my code for creating Texture2D from depthFrame of Kinect :
private short[] depthData;
private Texture2D depthTexture;
Color[] depthTextureData;
if (null == this.depthData || this.depthData.Length != frame.PixelDataLength)
{
this.depthData = new short[frame.PixelDataLength];
this.depthTexture = new Texture2D(
this.GraphicsDevice,
frame.Width,
frame.Height,
false,
SurfaceFormat.Bgra4444);
this.backBuffer = new RenderTarget2D(
this.GraphicsDevice,
frame.Width,
frame.Height,
false,
SurfaceFormat.Color,
DepthFormat.None,
this.GraphicsDevice.PresentationParameters.MultiSampleCount,
RenderTargetUsage.PreserveContents);
}
frame.CopyPixelDataTo(this.depthData);
depthTextureData = new Color[frame.Width * frame.Height];
depthTexture.GetData(depthTextureData);
I get an error on depthTexture.GetData(depthTextureData);
and it says :
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.Xna.Framework.Graphics.dll Additional information: The type you are using for T in this method is an invalid size for this resource.
anybody knows what is the issue ?
Upvotes: 2
Views: 3970
Reputation: 311
actually my datatype was Color, but I have to use Bgra4444. to use this datatype I have to use namespace Microsoft.Xna.Framework.Graphics.PackedVector and then make an array of Bgra4444 and use ToVector4() to convert a float vector (r, g, b, a) to use float vector of the array.
Upvotes: 1
Reputation: 3185
Basically, the GetData method asks for an array to receive data.
According to the MSDN, this method can throw two different exceptions: ArgumentNullException and/or InvalidOperationException.
If you are getting the first one, it probably is because the value of depthTextureData
is null by the time you use it with GetData
.
If this is not the case, have you tried specifying the type of T
when calling the method, as shown on the microsoft documentation?
backBufferData.GetData<Color>(...);
In that call, you are specifying that the type T
will be a Color
, and then you pass the instance of it, using whichever overload you like the most.
If you still can't solve the issue, you may want to have a look at the format type (full explanation of the problem an answer can be found here):
Here are the possible format types.
You are going to have to check texture.Format
and use the correct datastructure for its SurfaceFormat
.
For example.
var b = new Bgr565[result.Width * result.Height];
tex.SetData(b);
The below SurfaceFormat
have a corresponding value types that can be used.
Color
Bgr565
Bgra5551
Bgra4444
NormalizedByte2
NormalizedByte4
Rgba1010102
Rg32
Rgba64
Alpha8
Single
Vector2
Vector4
HalfSingle
HalfVector2
HalfVector4
The Dxt
format means that the texture is compressed and you are going to need to know the size after the it gets compressed, get the data and then decompress it.
You may be able to find a DXT1 and DXT5 decompression library somewhere. Unfortunately I can't find anything managed so unsafe C# code is probably the best bet for converting it over. According to Wikipedia, 16 pixels are stored in 8 bytes which makes have a byte per pixel so theoretically byte[] data = new byte[(Width * Height) / 2]
should work for extracting the data.
Dxt1
Dxt3
Dxt5
This one is a special case just use HalfVector4
for the type and you are fine.
HdrBlendable
Upvotes: 2