How to save a snapshot of the WebcamTexture?

I’m having trouble finding a way to take snaps of my WebcamTexture every x seconds. I want to directly save the data from the Webcam directly to a PNG.

So far I’ve tried a technique with a plane that fills the screen with the WebcamTexture assigned. Every x seconds I can do Application.CaptureScreenshot(fileName);, but I would like a more direct way to save the pixels to a PNG format.

Anybody got some ideas? Thanks

In the meantime, I figured it out. It’s a lot easier than I thought.
Here’s my solution, maybe someone else can also use this. Cheers :wink:

WebCamTexture _CamTex;
private string _SavePath = "C:/WebcamSnaps/";
int _CaptureCounter = 0;

void TakeSnapshot()
    Texture2D snap = new Texture2D(_CamTex.width, _CamTex.height);
    System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png", snap.EncodeToPNG());

Hi @Tim-Michels I found your answer pretty good and useful :slight_smile: just for future reference I did something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using UnityEngine;

public class CameraTest : MonoBehaviour {

    public RawImage rawimage;  //Image for rendering what the camera sees.
    WebCamTexture webcamTexture = null;

    void Start()
        //Save get the camera devices, in case you have more than 1 camera.
        WebCamDevice[] camDevices = WebCamTexture.devices;
        //Get the used camera name for the WebCamTexture initialization.
        string camName = camDevices[0].name;
        webcamTexture = new WebCamTexture(camName);

        //Render the image in the screen.
        rawimage.texture = webcamTexture;
        rawimage.material.mainTexture = webcamTexture;

    void Update()
        //This is to take the picture, save it and stop capturing the camera image.

    void SaveImage()
        //Create a Texture2D with the size of the rendered image on the screen.
        Texture2D texture = new Texture2D(rawimage.texture.width, rawimage.texture.height, TextureFormat.ARGB32, false);
        //Save the image to the Texture2D

        //Encode it as a PNG.
        byte[] bytes = texture.EncodeToPNG();
        //Save it in a file.
        File.WriteAllBytes(Application.dataPath + "/images/testimg.png", bytes);

This is used to show you what the picture looks like without opening it before :slight_smile: I hope this can be useful for someone else.

can anyone help me with how to convert the saved image to black and white

above shows the effect needed for unity3d