c# - 如何翻转内存中的图像?

标签 c# image xna flip texture2d

我想翻转图像并为该翻转图像创建一个新的 Texture2D。

我做了一些研究,发现这段代码会创建一个新的 Texture2D;

RenderTarget2D newTarget = new RenderTarget2D(GraphicsDevice, partWidth, partHeight)

GraphicsDevice.SetRenderTarget(newTarget);

SpriteBatch.Draw(original, Vector2.Zero, partSourceRectangle, ... )

GraphicsDevice.SetRenderTarget(null);

Texture2D newTexture = (Texture2D)newTarget;

我知道 newTexture 很容易从内存中删除,所以我被告知我需要使用 getData/SetData 来创建更永久的纹理。谁能告诉我执行此操作的具体语法?

最佳答案

下一个方法将翻转的纹理保存到新的 texture2D:

    public Texture2D SaveAsFlippedTexture2D(Texture2D input, bool vertical, bool horizontal)
    {
        Texture2D flipped = new Texture2D(input.GraphicsDevice, input.Width, input.Height);
        Color[] data = new Color[input.Width * input.Height];
        Color[] flipped_data = new Color[data.Length];

        input.GetData<Color>(data);

        for (int x = 0; x < input.Width; x++)
        {
            for (int y = 0; y < input.Height; y++)
            {
                int index = 0;
                if (horizontal && vertical)
                    index = input.Width - 1 - x + (input.Height - 1 - y) * input.Width;
                else if (horizontal && !vertical)
                    index = input.Width - 1 - x + y * input.Width;
                else if (!horizontal && vertical)
                    index = x + (input.Height - 1 - y) * input.Width;
                else if (!horizontal && !vertical)
                    index = x + y * input.Width;

                flipped_data[x + y * input.Width] = data[index];
            }
        }

        flipped.SetData<Color>(flipped_data);

        return flipped;
    }  

例子: 加载我们的纹理然后使用该方法,将我们的纹理作为参数传递以将新的翻转纹理返回到另一个纹理。您也可以在游戏 Update() 方法中加载您的内容。

    Texture2D texture;
    Texture2D flippedTextureHorizontal;
    Texture2D flippedTextureVertical;
    Texture2D flippedTextureVerticalHorizontal;
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        texture = Content.Load<Texture2D>("kitty-cat");
        flippedTextureHorizontal = SaveAsFlippedTexture2D(texture, false, true);
        flippedTextureVertical = SaveAsFlippedTexture2D(texture, true, false);
        flippedTextureVerticalHorizontal = SaveAsFlippedTexture2D(texture, true, true);
    }

绘制方法:

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise);
        spriteBatch.Draw(texture, Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        spriteBatch.Draw(flippedTextureHorizontal, new Vector2(texture.Width + offset, 0), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        spriteBatch.Draw(flippedTextureVertical, new Vector2(texture.Width * 2 + offset * 2, 0), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        spriteBatch.Draw(flippedTextureVerticalHorizontal, new Vector2(texture.Width * 3 + offset * 3, 0), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }

输出: Output

可以找到算法Here以及。 通过使用下面的代码同时进行水平和垂直翻转可以实现与上述相同的结果: 但不确定它会 100% 正确

test = new Texture2D(GraphicsDevice, texture.Width, texture.Height);
int size = texture.Width * texture.Height;
Color[] data = new Color[size];
texture.GetData<Color>(data);
Array.Reverse(data, texture.Width, size - texture.Width);
test.SetData<Color>(data);

关于c# - 如何翻转内存中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22519730/

相关文章:

python - 在python中将数组显示为光栅图像

jquery - 加载图像时在图像内加载指示器

c# - 如何在正则表达式中搜索模式时忽略特定字符串?

c# - ToArray() 方法将所有实例克隆为最后一个

C# 和 Oracle 数字数据类型

由 C# 调整图像大小

python - 如何在 kivy python 中重新加载图像

xna - 如何免费制作 Xbox Live 独立游戏?

c# - 获取颜色坐标(如 TEXCOORD0)以进行模糊处理

c# - 如何将emgu图像转换为XNA Texture2D?