- Home /
Screenshot in WebGL
I'm working on a web build that allows users to pick from a variety of options and then screencaps the view to be saved and printed on a UI button press. I'm trying to do some testing using a local server, but I can't seem to get the script to write the .png. Wondering if this is an issue of not communicating with the local server properly or if my code is borked. This version is a copypaste of the API suggestion with a couple of modifications, but I could be way off. I'm totally new to WWW stuff. If anyone could help point me in the right direction I'd be grateful.
public class SaveScreenshot : MonoBehaviour {
public string path = "";
public string screenShotURL= "http://localhost/screenshots/ScreenCapture.png";
// Use this for initialization
void Start () {
}
public void ScreenShot(){
filename = "Buttonshot_{0}" + ".png";
StartCoroutine(UploadPNG());
}
void update(){
}
IEnumerator UploadPNG() {
// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG ();
//save locally
//path = Application.persistentDataPath + "test.png";
//System.IO.File.WriteAllBytes(path, bytes);
// Create a Web Form
WWWForm form = new WWWForm ();
form.AddField ("frameCount", Time.frameCount.ToString ());
form.AddBinaryData ("fileUpload", bytes, "screenShot.png", "image/png");
// Upload to a cgi script
WWW w = new WWW (screenShotURL, form);
yield return w;
if (!string.IsNullOrEmpty (w.error)) {
print (w.error);
} else {
print ("Finished Uploading Screenshot");
}
}
}
Your answer
Follow this Question
Related Questions
what is needed to get WWW Post working correctly 2 Answers
Post image to facebook wall 0 Answers
How to upload/download video from/to mobile 1 Answer
seperate binary retrieved from sql server 1 Answer
http request over an https webgl 1 Answer