简体   繁体   English

XNA Texture2D GetData

[英]XNA Texture2D GetData

this is my code for creating Texture2D from depthFrame of Kinect : 这是我从Kinect的depthFrame创建Texture2D的代码:

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); 我在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. Microsoft.Xna.Framework.Graphics.dll中发生了类型为'System.ArgumentException'的未处理异常。其他信息:您在此方法中用于T的类型对该资源来说是无效的大小。

anybody knows what is the issue ? 有人知道这是什么问题吗?

Basically, the GetData method asks for an array to receive data. 基本上, GetData方法要求一个数组来接收数据。

According to the MSDN, this method can throw two different exceptions: ArgumentNullException and/or InvalidOperationException . 根据MSDN,此方法可以引发两个不同的异常: ArgumentNullException和/或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 . 如果您正在获取第一个,则可能是因为在与GetData一起使用时, depthTextureData值为null。

If this is not the case, have you tried specifying the type of T when calling the method, as shown on the microsoft documentation ? 如果不是这种情况,是否在调用方法时尝试指定T的类型,如Microsoft文档中所示?

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. 在该调用中,您指定类型TColor ,然后使用最喜欢的重载传递它的实例。

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 . 您将必须检查texture.Format并为其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. 下面的SurfaceFormat具有可以使用的对应值类型。

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. Dxt格式表示纹理已压缩,压缩后需要知道其大小,获取数据然后再解压缩。

You may be able to find a DXT1 and DXT5 decompression library somewhere. 您也许可以在某处找到DXT1和DXT5解压缩库。 Unfortunately I can't find anything managed so unsafe C# code is probably the best bet for converting it over. 不幸的是,我找不到任何可管理的内容,因此不安全的C#代码可能是将其转换的最佳选择。 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. 根据Wikipedia的介绍,16个像素存储在8个字节中,这使得每个像素有一个字节,因此从理论上讲byte[] data = new byte[(Width * Height) / 2]应该可以提取数据。

Dxt1
Dxt3
Dxt5

This one is a special case just use HalfVector4 for the type and you are fine. 这是一种特殊情况,只需使用HalfVector4作为类型,就可以了。 HdrBlendable HdrBlendable

actually my datatype was Color, but I have to use Bgra4444. 实际上我的数据类型是Color,但是我必须使用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. 要使用此数据类型,我必须使用命名空间Microsoft.Xna.Framework.Graphics.PackedVector,然后制作Bgra4444数组,并使用ToVector4()转换浮点向量(r,g,b,a)以使用浮点向量阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM