Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When using this code:

public static Texture2D MakeTexRed(Texture2D tex)
    {
        Texture2D drawTex = new Texture2D(Game1.graphics.GraphicsDevice, tex.Width, tex.Height);
        Color[] data = new Color[tex.Width * tex.Height];
        tex.GetData<Color>(data);
        for (int i = 0; i < data.Length; i++)
        {
            data[i].R = (byte)((data[i].R + 255) / 2);
            data[i].G /= 2;
            data[i].B /= 2;
        }
        drawTex.SetData(data);

        return drawTex;
    }

I'm getting this exception:

The type you are using for T in this method is an invalid size for this resource.

The size of the texture I'm using is 64x64 pixel.

When using this line:

Texture2D drawTex = new Texture2D(Game1.graphics.GraphicsDevice, tex.Width, tex.Height, false, SurfaceFormat.Color);

it still doesn't work.

share|improve this question
    
Have you tried specifying SurfaceFormat.Color in the Texture2D constructor? –  Simon Moles Apr 18 '13 at 16:16
    
Still doesn't work. –  user1990950 Apr 18 '13 at 16:24
    
Sure you're not referencing the Color type from a different namespace? It's the Microsoft.Xna.Framework namespace? –  Byte56 Apr 18 '13 at 16:35
    
Yes, replacing Color with Microsoft.Xna.Framework.Color also didn't help. –  user1990950 Apr 18 '13 at 16:51
2  
You're calling GetData() on the texture being passed in, not the one you're creating. How is the texture being passed in constructed? Where does it come from? –  Cole Campbell Apr 18 '13 at 19:16

2 Answers 2

up vote 1 down vote accepted

So here are the possible format types.

you are going to have to check tex.Format and use the correct datastructure for tex's 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

For the Dxt formats means that the texture is compressed you are going to need to know the size after the texture is compressed get the data and then decompress it.

After some searching I found a DXT1 and DXT5 decompression library. 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
share|improve this answer
    
The format of the texture is Dxt1, how can I decompress it? –  user1990950 Apr 19 '13 at 14:17
    
From my understanding of Dxt1 you can use 'byte[] data = new byte[(Width * Height) / 2]' to extract the data. Then you need to convert the linked to unmanaged c++ library to C#. I edited more info into the answer. –  ClassicThunder Apr 19 '13 at 14:35
    
You might be able to draw the texture to a RenderTarget2D of the same size who has its SurfaceFormat set to Color and then unload the Dxt texture and replace it with the Color texture. –  ClassicThunder Apr 19 '13 at 14:39

Have your tried not specifying the T when calling GetData() ?

tex.GetData<Color>(data);

To :

tex.GetData(data);

The error you posted refers to the Color you pass as T.

share|improve this answer
    
He's doing Color[] data = new Color[tex.Width * tex.Height] so both lines of code will have exactly the same behavior. –  ClassicThunder Apr 19 '13 at 6:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.