- Home /
System.IO.File error, take a screenShot
I try to take a screen shot when the user presses' k. My worry now is that I get an error about using WriteAllBytes.
Assets / Scripts / Utility Scripts / HiResScreenShots.cs (37.40): error CS0117: System.IO.File 'does not Contain a definition for
WriteAllBytes'
using UnityEngine;
using System.Collections;
using System.IO;
public class HiResScreenShots : MonoBehaviour {
public int resWidth = 4096;//1600;//2550;
public int resHeight = 2232;//990;//3300;
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,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
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; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
//System.IO.File.WriteAllBytes(filename, bytes);
System.IO.File.WriteAllBytes(Application.dataPath + "/screenshots/screen" + System.DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".png", bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
takeHiResShot = false;
}
}
}
Need help :s
3.3.0f4, i have some problem to download the last one on HotSpot connexion :s
That has WriteAllBytes. Try setting the .net compatibility to 2.0 full.
I'm not sure to understand what setting you are talking about. :s
In the player settings, you have API compatibility, with the choices being .NET 2.0 or .NET 2.0 subset. However I don't think that's it, because I tried it with Unity 3.3 using subset, and it works fine here.
Answer by Kalu · Feb 24, 2012 at 02:46 PM
Fixed with :
Unity:
public IEnumerator take_screen_shot () {
// We should only read the screen after all rendering is complete yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D( width-210, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width-210, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
// Create a Web Form
WWWForm form = new WWWForm();
if(idmeuble == 0){
int nb1 = Random.Range(9, 99);
int nb2 = Random.Range(0, 9);
int nb3 = Random.Range(100, 999);
string s_idmeuble = nb1.ToString()+nb2.ToString()+nb3.ToString();
idmeuble = int.Parse(s_idmeuble);
}
string s_name = idmeuble + "_-_" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
s_name = s_name+".png";
ref_link_screenshot = s_name;
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("file", bytes, s_name, "image/png");
// Upload to a cgi script
WWW w = new WWW(screenShotURL, form);
yield return w;
if (w.error != null){
Debug.Log(w.error);
mssg_debug += (w.error);
}
else{
mssg_debug += " upload ok ";
Main otherScript = GetComponent<Main>();
otherScript.cur_screenshot_statu = otherScript.screen_shot_done;
otherScript.b_capture_ok = true;
}
if(w.text != null){ mssg_debug += w.text; }
}
PHP:
<?php
$message = "Error";
echo $_FILES["file"]["type"];
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000000000)) {
if ($_FILES["file"]["error"] > 0) {
$message = "Error";
//echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
//echo "Upload: " . $_FILES["file"]["name"] . "";
//echo "Type: " . $_FILES["file"]["type"] . "";
//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
if (file_exists("dir_screen_shot/" . $_FILES["file"]["name"]))
{
$message = "Error";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "dir_screen_shot/" . $_FILES["file"]["name"]);
$message = "Done";
}
}
} else { $message = "Error"; } //invalide File
echo $message;
?>
Answer by luizgpa · Jan 22, 2012 at 08:26 PM
Probably the current platform of your project is "Web player", witch doesn't have this method. To change it go to "File-> Build Settings", select "PC and Mac Standalone" and click "Switch Platform".
@$$anonymous$$alu: There is no local file access in web apps for security reasons.
So how can i take a screen shot? Sorry i already had this problem to editing xml ..
I don't think Unity can do that directly, but you could use Flash to save the screenshot. The steps would be:
1- encode your byte[] to a string in Base64
2- pass the string to the Flash player via Javascript (browser's javascript, not UnityScript)
3- get the string and decode it to a ByteArray
4- use FileReference.save to ask the user where to save the file
It looks good. I'll try as soon as I have time Thx! I'll tell you if it worked