- Home /
 
Saving Texture2D as a PNG image to HDD in Standalone build.
Hey guys! Do you know about any good way to encode and save a PNG image to local HDD in a Standalone build? I know it isn't possible in WebPlayer, but it hopefully is in Standalone.
David
Answer by Fabkins · Apr 28, 2012 at 02:42 PM
Here you go:
  import System.IO;
       
  function SaveTextureToFile( texture: Texture2D,fileName)
  {
     var bytes=texture.EncodeToPNG();
     var file = new File.Open(Application.dataPath + "/"+fileName,FileMode.Create);
     var binary= new BinaryWriter(file);
     binary.Write(bytes);
     file.Close();
  }
 
               Call it using:
  var myTexture: Texture2D;
  
  function Startup()
  {
      SaveTextureToFile( myTexture,"picture.png");
  }
 
               Enjoy :)
NOTE: The texture has to be readable. If its a texture that you've made should be ok but if you have imported then you need to make it readable.
You sir, are awesome and have all my applauds :). Thanks a lot!
A slightly shorter version, in case it is useful for anyone:
System.IO.File.WriteAllBytes("picture.png", myTexture.EncodeToPNG());
     System.IO.File.WriteAllBytes(Application.dataPath + "/"+"picture5886.png", texture2.EncodeToPNG());    //app path n1!    
 
                 There is no: var file = new File.Open(...) It is a static method, so you call: var file = File.Open(...)
Answer by shacharoz · Dec 07, 2020 at 10:56 PM
 string file_path = "PATH/FILENAME.PNG";
 byte[] filedata = texture2d.EncodeToPNG();
 using (var fs = new FileStream(file_path, FileMode.Create, FileAccess.Write))
 {
       fs.Write(fileData, 0, fileData.Length);
 }
 
 
              Answer by devilkkw · Apr 28, 2012 at 11:45 AM
 Application.CaptureScreenshot("Screenshot.png");
 
               thath save file named Screenshot.png in a folder where u have executable. Best way is making function for multiple screenshot for don't overwrite exiting shot.and i use thath on conditional function with a keyboard press.
Thanks, interesting answer, it doesnt save a png of a texture, only of a screencap... just a note to say it didnt work with your example from the unity reference, which doesnt work, i had to do:
Application.CaptureScreenshot("Assets/savedmeshes/cappedscreens/ " + "Screenshot2.png");
you wuold have to do:
Application.CaptureScreenshot("Assets/ " + "Screenshot2.png");
did this too oddly engouh:
 function sshot()//:Texture2D
 { 
 // $$anonymous$$ake a new texture of the right size and
 // read the camera image into it.
 var tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
 yield  WaitForEndOfFrame();
 tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
 tex.Apply();
 var bytes = tex.EncodeToPNG();
 
 SaveTextureToFile(tex,"alcove.png");
 //Destroy (tex);
 
 //return tex;
 }
 
  
  function SaveTextureToFile( texture: Texture2D,fileName)
  {
     var bytes=texture.EncodeToPNG();
     var file = new File.Open(Application.dataPath + "/"+fileName,File$$anonymous$$ode.Create);
     var binary= new BinaryWriter(file);
     binary.Write(bytes);
     file.Close();
  }
  
 
                 Your answer
 
             Follow this Question
Related Questions
Saving and Loading XML in Standalone 1 Answer
Can Unity use .ini files? 1 Answer
Write text in browser? 0 Answers
Where to save games on desktop? 0 Answers
Save to a separate system folder 1 Answer