- Home /
Problems using system.IO to create screenshots of camera
Hello
I'm using the following script. The same as described in This thread. I keep getting this error.
"DirectoryNotFoundException: Could not find a part of the path "/Users/Admin/Documents/Unity /Projects/VJ/Assets/screenshots/screen_4096x2232_71.png". System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/FileStream.cs:292) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor"
and here's the script.
using UnityEngine;
using System.Collections;
using System.IO;
public class HiResScreenShots : MonoBehaviour {
public int resWidth = 4096;
public int resHeight = 2232;
private bool takeHiResShot = false;
public static string ScreenShotName(int width, int height) {
return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", Application.dataPath, width, height, Time.frameCount);
}
public void TakeHiResShot() {
takeHiResShot = true;
}
void LateUpdate() {
takeHiResShot |= Input.GetKeyDown("k");
if (takeHiResShot) {
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
takeHiResShot = false;
}
}
}
Hello,You could solve the problem? because I have the same problem when I try to save a picture in a folder. What is de path from to save in a folder inside the project.
P.D: sorry for my bad english
@IgnacioGV - The folder structure you see in the Editor does not exist at runtime. Strea$$anonymous$$gAssets is the only folder that is carried into the install. Depending on what you are trying to do, you might also take a look at Application.persistentDataPath. If/where you can write will depend on the platform.
Your answer
