Ballz.GameSession.Renderer.GuiRenderer.ToTexture C# (CSharp) Method

ToTexture() public method

public ToTexture ( ) : Microsoft.Xna.Framework.Graphics.Texture2D
return Microsoft.Xna.Framework.Graphics.Texture2D
        public Texture2D ToTexture()
        {
            if (Texture == null)
                Texture = new Texture2D(Ballz.The().GraphicsDevice, Browser.Width, Browser.Height, false, SurfaceFormat.Bgra32);

            var bitmap = ToBitmap();
            if (bitmap != null)
            {
                var bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                byte[] data = new byte[bitmap.Width * bitmap.Height * 4];
                Marshal.Copy(bitmapdata.Scan0, data, 0, data.Length);
                bitmap.UnlockBits(bitmapdata);

                byte[] fixeddata = new byte[bitmap.Width * bitmap.Height * 4];

                for(int i = 0; i < data.Length; i += 4)
                {
                    bool transparent = data[i + 0] == 255 && data[i + 1] == 0 && data[i + 2] == 255;
                    fixeddata[i + 0] = data[i + 0];
                    fixeddata[i + 1] = data[i + 1];
                    fixeddata[i + 2] = data[i + 2];
                    fixeddata[i + 3] = transparent ? (byte)0 : (byte)255;
                }

                Texture.SetData(fixeddata);

                return Texture;
            }
            else
                return null;
        }