Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity c#, take screenshot and save to file as jpg

I am trying to Take a screencapture and save it to a file in jpg format. I am following this example.

http://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html

This is what I have so far:

    string jpgFile = Application.persistentDataPath + "/scrn-1.jpg";
    Texture2D tex = new Texture2D (Screen.width, Screen.height);
    tex.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    tex.Apply ();
    var bytes = tex.EncodeToJPG();
    Destroy (tex);
    System.IO.File.WriteAllBytes(jpgFile, bytes);

I have found that running this in Unity on iOS gives me:

JPEG parameter struct mismatch: library thinks size is 372, caller expects 360

However if I change the conversion to tex.EncodeToPNG(); and change the file name to .png everything works fine. I am not sure how to proceed any assistance would be appreciated. Thanks.

like image 329
running-codebase Avatar asked Mar 19 '23 07:03

running-codebase


2 Answers

I found this, which lets you save it as .jpg. I normally just use capture screen and save as .png.

Here you go:

using UnityEngine;
using System.Collections;

public class HiResScreenShots : MonoBehaviour {
    public int resWidth = 2550; 
    public int resHeight = 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);
            Debug.Log(string.Format("Took screenshot to: {0}", filename));
            takeHiResShot = false;
        }
    }
}

If you just want any kind of picture I highly recommend this:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour 
{
    void OnMouseDown() 
    {
        Application.CaptureScreenshot("Screenshot.png");
    }
}

Simple and effective.

like image 160
apxcode Avatar answered Apr 02 '23 02:04

apxcode


I actually needed something like this in these today, but not saving It. Unity doesn't provide us a default method to return the image instantly as a byte, this is how I solved my problem.

internal class ScreenCapture : MonoBehaviour
{
    internal byte[] Image = null;

    internal void SaveScreenshot()
    {
        StartCoroutine(SaveScreenshot_ReadPixelsAsynch());
    }

    private IEnumerator SaveScreenshot_ReadPixelsAsynch()
    {
        //Wait for graphics to render
        yield return new WaitForEndOfFrame();

        //Create a texture to pass to encoding
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

        //Put buffer into texture
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

        //Split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
        yield return 0;

        byte[] bytes = texture.EncodeToPNG();
        Image = bytes;

        //Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
        DestroyObject(texture);
    }
}

Usage:

internal static ScreenCapture Capture;



if (Capture == null)
{
    Capture = Camera.main.gameObject.AddComponent<ScreenCapture>();
}
Capture.SaveScreenshot();

// if you wish to save:
System.IO.File.WriteAllBytes("filename.png", Capture.Image);
like image 28
DreTaX Avatar answered Apr 02 '23 02:04

DreTaX