Mighty forum!
I’m looking for ways to save Texture2D as PNG to disk. The goal is to record 2nd camera out as a video clip in run time, and my current process as below:
- Once start capturing: use Texture2D ReadPixels from RenderTexture of target Camera
- Get byte from Texture2D with EncodeToPNG()
- File.WriteAllBytes to save the byte to png
- Once stop capturing: use ffmpeg bat file to convert image sequences to video
I can get EncodeToPNG to work but the performance is too bad for the game play, and it can only run in main Thread. So I’m wondering if there’s other ways to encode the Texture2D? That can be put in new Thread?
I’ve also tried System.Drawing.dll, using codes as below:
Texture2D tex = new Texture2D (renderWidht, renderHeight, TextureFormat.ARGB32, false);
tex.ReadPixels (new Rect(0, 0, renderWidht, renderHeight), 0, 0);
byte[] bytes = tex.GetRawTextureData();
using( MemoryStream memStream = new MemoryStream(bytes) )
{
Image image = Image.FromStream (memStream);
image.Save (filename, System.Drawing.Imaging.ImageFormat.Png);
}
But got an error on Image.FromStream part, as below, and I’m guessing it’s because the byte from GetRawTextureData() is not applicable here??
ArgumentException: A null reference or invalid value was found [GDI+ status: InvalidParameter]
I’m new to these image processing stuff, and have been struggled for days. Any hints and helps appreciated! Thanks!