【问题标题】:Error when trying to save XNA Texture2D: System.Exception: 'Texture surface format not supported'尝试保存 XNA Texture2D 时出错:System.Exception:“不支持纹理表面格式”
【发布时间】:2020-11-16 07:51:57
【问题描述】:

我想将Texture2D 保存到.png 文件中。 Texture2D 的表面格式是SurfaceFormat.Dxt1,我相信这就是SaveAsPng 抛出错误System.Exception: 'Texture surface format not supported' 的原因

Stream stream = File.Create("file.png");
tex.SaveAsPng(stream, tex.Width, tex.Height);
stream.Dispose();

将 DxT1 格式保存为 png 的最佳方法是什么?

【问题讨论】:

    标签: c# graphics xna png textures


    【解决方案1】:

    MonoGame 中的Texture2D.SaveAsPng 不支持 DXT 格式,但您可以通过 GPU 渲染并保存它。例如:

    var gpu = texture.GraphicsDevice;
    using (var target = new RenderTarget2D(gpu, texture.Width, texture.Height))
    {
        gpu.SetRenderTarget(target);
        gpu.Clear(Color.Transparent); // set transparent background
    
        using (SpriteBatch batch = new SpriteBatch(gpu))
        {
            batch.Begin();
            batch.Draw(texture, Vector2.Zero, Color.White);
            batch.End();
        }
    
        // save texture
        using (Stream stream = File.Create("file.png"))
            target.SaveAsPng(stream, texture.Width, texture.Height);
    
        gpu.SetRenderTarget(null);
    }
    

    一些注意事项:

    • 这相对较慢,因此您可能希望将Texture.SaveAsPng 用于它支持的纹理格式。
    • 您不能在 XNA 绘制循环期间执行此操作,因为此时 GPU 将被占用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2017-08-07
      • 1970-01-01
      相关资源
      最近更新 更多