title: Take photo in unity c#
body: <p>I'm trying to build a program that takes your photo and places it with a different background, like a monument or so. So far, I was able to turn the camera on when I start the project with this code</p>
  
  <pre><code> webcamTexture = new WebCamTexture();
   rawImage.texture = webcamTexture;
   rawImage.material.mainTexture = webcamTexture;
   webcamTexture.Play();
   Texture2D PhotoTaken = new Texture2D (webcamTexture.width, webcamTexture.height);
   PhotoTaken.SetPixels (webcamTexture.GetPixels ());
   PhotoTaken.Apply ();
  </code></pre>
  
  <p>However, I can't take a screenshot or photo because it always ends up all black. I've tried different codes but nothing is working. Can someone please help? Thanks</p>
  
  <p>EDIT </p>
  
  <p>After some tries, this is the code I have:</p>
  
  <pre><code>WebCamTexture webcamTexture;
  public RawImage rawImage;
  
  
  void Start () {
      webcamTexture = new WebCamTexture();
      rawImage.texture = webcamTexture;
      rawImage.material.mainTexture = webcamTexture;
      webcamTexture.Play();
  
      RenderTexture texture= new RenderTexture(webcamTexture.width, webcamTexture.height,0); 
      Graphics.Blit(webcamTexture, texture);
  
      Button btn = yourButton.GetComponent&lt;Button&gt;();
      btn.onClick.AddListener(OnClick);
  
  }
  
  public IEnumerator Coroutine(){
  
      yield return new WaitForEndOfFrame ();
  }
  
  public void OnClick() {
  
      var width = 767;
      var height = 575;
  
      Texture2D texture = new Texture2D(width, height);
      texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
      texture.Apply();
  
      // Encode texture into PNG
      var bytes = texture.EncodeToPNG();
      //Destroy(texture);
  
  
      File.WriteAllBytes (Application.dataPath + "/../SavedScreen.png", bytes);
  
      }
  </code></pre>
  
  <p>and with this next code the screenshot is taken, but it takes a photo of the whole thing, and not just a bit of the screen.</p>
  
  <pre><code>void Start()
  {
      // Set the playback framerate!
      // (real time doesn't influence time anymore)
      Time.captureFramerate = frameRate;
  
      // Find a folder that doesn't exist yet by appending numbers!
      realFolder = folder;
      int count = 1;
      while (System.IO.Directory.Exists(realFolder))
      {
          realFolder = folder + count;
          count++;
      }
      // Create the folder
      System.IO.Directory.CreateDirectory(realFolder);
  }
  
  void Update()
  {
      // name is "realFolder/shot 0005.png"
      var name = string.Format("{0}/shot {1:D04}.png", realFolder, Time.frameCount);
  
      // Capture the screenshot
      Application.CaptureScreenshot(name, sizeMultiplier);
  }
  </code></pre>
  
  <p>}</p>