FlatRedBall.Graphics.Texture.ImageData.FromTexture2D C# (CSharp) Method

FromTexture2D() public static method

public static FromTexture2D ( Microsoft.Xna.Framework.Graphics.Texture2D texture2D ) : ImageData
texture2D Microsoft.Xna.Framework.Graphics.Texture2D
return ImageData
        public static ImageData FromTexture2D(Texture2D texture2D)
        {
#if DEBUG
            if (texture2D.IsDisposed)
            {
                throw new Exception("The texture by the name " + texture2D.Name + " is disposed, so its data can't be accessed");
            }
#endif

            ImageData imageData = null;
            // Might need to make this FRB MDX as well.
#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE

            switch (texture2D.Format)
            {
#if !XNA4 && !MONOGAME
                case SurfaceFormat.Bgr32:
                    {
                        Color[] data = new Color[texture2D.Width * texture2D.Height];
                        texture2D.GetData<Color>(data);

                        // BRG doesn't have alpha, so we'll assume an alpha of 1:
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i].A = 255;
                        }

                        imageData = new ImageData(
                            texture2D.Width, texture2D.Height, data);
                    }
                    break;
#endif
                case SurfaceFormat.Color:
                    {
                        Color[] data = new Color[texture2D.Width * texture2D.Height];
                        texture2D.GetData<Color>(data);

                        imageData = new ImageData(
                            texture2D.Width, texture2D.Height, data);
                    }
                    break;
                case SurfaceFormat.Dxt3:

                    Byte[] byteData = new byte[texture2D.Width * texture2D.Height];
                    texture2D.GetData<byte>(byteData);

                    imageData = new ImageData(texture2D.Width, texture2D.Height, byteData);

                    break;

                default:
                    throw new NotImplementedException("The format " + texture2D.Format + " isn't supported.");

                //break;
            }

#endif
            return imageData;
        }

Usage Example

Esempio n. 1
0
        public void Blit(Texture2D source, Rectangle sourceRectangle, Point destination)
        {
            ImageData sourceAsImageData = ImageData.FromTexture2D(source);

            Blit(sourceAsImageData, sourceRectangle, destination);
        }