c# - 尝试保存 XNA Texture2D 时出错:System.Exception:“不支持纹理表面格式”
问题描述
我想将 a 保存Texture2D
到.png
文件中。的表面格式Texture2D
是 a 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 的最佳方法是什么?
解决方案
Texture2D.SaveAsPng
MonoGame 中不支持 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 将被占用。
推荐阅读
- go - Grpc Go Generated.pb.go 导入未格式化
- ruby-on-rails - 在 Rails 中-如何从 X 列和总和 Y 列中获取不同的行
- scope - blazor 包含代码块故障 @{} 错误然后 {} 范围错误
- javascript - Vue JS 无限加载在 v-else 中不起作用
- python-3.x - 如果我们要读取此关闭的设备不可用,是否有 PURESNMP 库的功能可以延长 SNMP 查询?
- python - 类型错误:汤= BeautifulStoneSoup(page.text,'lxml')
- ios - 应用程序在后台时,AVAudioPlayer 一段时间后不播放
- r - 如何从 R 中的现有表在 R 中创建新表
- arrays - iOS/Swift:为什么 contains 不能检测 .firstIndex 函数中的子字符串?
- mysql - 我如何按 book_id 和 order_id 计算总数