I want to save a Texture2D
to a .png
file. The surface format of the Texture2D
is a SurfaceFormat.Dxt1
, which I believe is why SaveAsPng
is throwing an error System.Exception: 'Texture surface format not supported'
Stream stream = File.Create("file.png");
tex.SaveAsPng(stream, tex.Width, tex.Height);
stream.Dispose();
What's the best way to save a DxT1 format as a png?
Texture2D.SaveAsPng
in MonoGame doesn't support DXT formats, but you can render it through the GPU and save that instead. For example:
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);
}
Some caveats:
Texture.SaveAsPng
for the texture formats it does support.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.