Displaying RGB/depth images in XNA using C#

Try this function. Remember to compile with /unsafe.
byte[] bmpBytes; 
        Texture2D background; 
        private unsafe Texture2D GetDepthTextureMy() 
        { 
            this.depth.GetMetaData(depthMD); 
            lock (this) 
            { 
                int byteCount = depthMD.XRes * depthMD.YRes * 4; //4- bytes color representation - RGBA 


                if (bmpBytes == null || 
                    bmpBytes.Length != byteCount) 
                    bmpBytes = new byte[byteCount]; 

                fixed (byte* texturePointer = &bmpBytes[0]) 
                { 
                    ushort* pDepth = (ushort*)this.depth.GetDepthMapPtr().ToPointer(); 

                    int pointerWalker = 0; 
                    for (int y = 0; y < depthMD.YRes; ++y) 
                    { 
                        for (int x = 0; x < depthMD.XRes; ++x, ++pDepth) 
                        { 
                            byte pixel = (byte)(*pDepth / (float)depthMD.ZRes * 255f); 
                            texturePointer[pointerWalker] = pixel; 
                            texturePointer[pointerWalker + 3] = 255; // set alpha to 1 
                            pointerWalker += 4; 
                        } 
                    } 
                } 
            } 


            if (background == null) 
                background = new Texture2D(game.GraphicsDevice, depthMD.XRes, depthMD.YRes); 


            background.SetData(bmpBytes); 
            return background; 
        } 

here's a method in the same vein to get the RGB image:

public unsafe Texture2D GetImageTexture() 
        { 
            Texture2D imageTexture = null; 
            byte[] bmpBytes = null; 


            this.image.GetMetaData(imageMD); 
            lock (this) 
            { 
                int byteCount = imageMD.XRes * imageMD.YRes * 4;    // 4-byte color representation - RGBA 

                if (bmpBytes == null || bmpBytes.Length != byteCount) 
                    bmpBytes = new byte[byteCount]; 


                fixed (byte* texturePointer = &bmpBytes[0]) 
                { 
                    RGB24Pixel* pImage = (RGB24Pixel*)this.image.GetImageMapPtr().ToPointer(); 
                    int pointerWalker = 0; 

                    for (int y = 0; y < imageMD.YRes; ++y) 
                    { 
                        for (int x = 0; x < imageMD.XRes; ++x, + 
+pImage, pointerWalker += 4) 
                        { 
                            texturePointer[pointerWalker] = pImage->nRed; 
                            texturePointer[pointerWalker + 1] = pImage->nGreen; 
                            texturePointer[pointerWalker + 2] = pImage->nBlue; 
                            texturePointer[pointerWalker + 3] = 255;    //Set alpha to 1 
                        } 
                    } 
                } 
            } 

            if (imageTexture == null) 
                imageTexture = new Texture2D(game.GraphicsDevice, imageMD.XRes, imageMD.YRes); 

            imageTexture.SetData(bmpBytes); 
            return imageTexture; 
        } 



你可能感兴趣的:(image,C#,null,float,byte)