• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Giusort · Jul 14, 2017 at 03:36 PM · savescreenshotpnguploadjpg

Upload images on server

Hi,

I need to take jpg or png screenshots at time intervals and store it on the file system and on a server; with this code I solved the file system saving:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 
 public class ScreenRecorder : MonoBehaviour {
     public int framerate = 30;
     public bool JPGImage = true;
     public int superSize;
 
     int frameCount = 0;
     bool recording = false;
     int ImageCounter = 0;
     int TimeCounter = 0;
     string imageFileDir = "";
 
     void Start () {
         imageFileDir = Application.persistentDataPath+"/Capture/";
         frameCount = 0;
     }
 
     public void StartStopRecording () {
         GameObject StartStopButtonGO;
 
         StartStopButtonGO = GameObject.Find ("RecText");
 
         if (!recording) {
             ImageCounter = 0;
             SetImageCounter (ImageCounter);
             SetTimeCounter (TimeCounter);
             DeleteImageDir (imageFileDir);
         }
 
         recording = !recording;
         if (recording) {
             Time.captureFramerate = framerate;
             frameCount = -1;
             StartStopButtonGO.GetComponent<Text>().text = "STOP";
             Debug.Log ("Start recording");
         } else {
             StartStopButtonGO.GetComponent<Text>().text = "REC";
             Debug.Log ("Stop recording");
         }
     }
 
     void Update () {
         StartCoroutine(TakeShoot ());
     }
 
     void SetImageCounter (int ImageCount) {
         GameObject ImageCounterGO;
 
         ImageCounterGO = GameObject.Find ("ImageCount");
         ImageCounterGO.GetComponent<Text> ().text = ImageCount.ToString ();
     }
 
     void SetTimeCounter (int TimeCount) {
         GameObject TimeCounterGO;
 
         TimeCounterGO = GameObject.Find ("TimeCount");
         TimeCounterGO.GetComponent<Text> ().text = TimeCount.ToString ();
     }
 
     IEnumerator TakeShoot() {
         byte[] bytes;
         string fullImageFileName = imageFileDir + "/";
         string imageName = "frame";
 
         if (recording) {
             if (frameCount > 0 && frameCount % 30 == 0) {
 
                 yield return new WaitForEndOfFrame ();
 
                 // Create a texture the size of the screen, RGB24 format
                 int width = Screen.width;
                 int height = Screen.height - 100; // Screen.height-button height
 
                 Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
 
                 // Read screen contents into the texture
                 tex.ReadPixels (new Rect (0, 100, width, height), 0, 0);
                 tex.Apply ();
 
                 imageName = "frame" + frameCount.ToString ("0000");
 
                 if (JPGImage) {
                     bytes = tex.EncodeToJPG (100);
                     imageName += ".jpg";
                 } else {
                     bytes = tex.EncodeToPNG ();
                     imageName += ".png";
                 }
 
                 fullImageFileName += imageName;
                 Destroy (tex);
 
                 File.WriteAllBytes (fullImageFileName, bytes);
 
                 ImageCounter++;
                 SetImageCounter (ImageCounter);
 
                 yield return null;
             }
             frameCount++;
 
             if (frameCount % 60 == 0) {
                 TimeCounter++;
                 SetTimeCounter (TimeCounter);
             }
         }
     }
 
     void DeleteImageDir(string ImagePath) {
         if (Directory.Exists (ImagePath)) {
             var OldImageFiles = Directory.GetFiles(ImagePath);
             for (int i = 0; i < OldImageFiles.Length; i++)
                 File.Delete (OldImageFiles [i]);
         } else {
             Directory.CreateDirectory (ImagePath);
         }
     }
 }

 

Now I'm trying to upload images on a server and I found this examples:

https://docs.unity3d.com/ScriptReference/WWWForm.html

but what about the PERL script "http://www.my-server.com/cgi-bin/screenshot.pl"?

Or this: https://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html

but what about the CGI script "WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);"?

So, there is a way to load an image or, in general, a file on a server?

Thank you!

[EDIT] Finally I found these examples:

https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx https://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class

This way I can upload files but I need also to show the upload progress; in the second example I can add

Debug.Log (string.Format("Progress: {0}", ((100*localFileStream.Position) / localFileStream.Length)));

in requestStream.Write cycle but the upload process stucks the app so messages are written only at the end of the entire upload process.

Some idea?

Thank you

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Linear Color Space - Screencapture (Dark Screenshots) 2 Answers

How to Get a JPG/ PNG Render from a Selected Camera ? 0 Answers

Issues when taking screenshot from multiple cameras. 0 Answers

How Do I Upload To An Email Address Like Gmail Or Outlook In Unity 3d Either from PC or Phone andr or ios 0 Answers

Save a Sprite object as a Png into a folder with a click of a button. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges