Search Unity

Taking screenshot

Discussion in 'Windows' started by Default117, Oct 17, 2013.

  1. Default117

    Default117

    Joined:
    Mar 13, 2011
    Posts:
    134
    Hi guys,
    Hoping someone can point me in the right direction with this as i'm running out of ideas. I'm unable to take a screenshot or store it in a metro app. I've tried the normal Applicaiton.CaptureScreenshot code which results in:
    Code (csharp):
    1. about to send file over playerconnection C:/Projects/ProjectName/Export/ProjectName/bin/x86/Debug/AppX/Data/Test_1887.png  with length 71053
    2. Failed to store screen shot
    /edit/
    Problem solved, i ended up trying ReadPixels (has some problem with this as well, where using a render texture didn't include OnGUI textures, and plain ReadPixels on a Texture2D resulted in black pixels).
    Turns out i didnt yield a frame before reading pixels.

    That being said, any idea why Application.CaptureScreenshot won't work?
     
    Last edited: Oct 17, 2013
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Can you show the actual code that does capture the screenshot?
     
  3. Default117

    Default117

    Joined:
    Mar 13, 2011
    Posts:
    134
    So the code I'm using now to capture the screenshot is simply:

    Code (csharp):
    1.  
    2. string filename = "Test_" + Random.Range(0, 9999) + ".png";
    3.         string fullpath = "";
    4.  
    5.         yield return new WaitForEndOfFrame();
    6.         Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    7.         tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    8.         tex.name = "ScreenScrape";
    9.         tex.Apply();
    10.         byte[] bytes = tex.EncodeToPNG();
    11.         Destroy(tex);
    After I have the bytes, I'm using the data writer to write it to the pictures library:
    Code (csharp):
    1.  
    2.  
    3. private static async void _ByteArrayToBitmapImage(string fileName, byte[] byteArray)
    4.         {
    5.             //Find the pictures folder
    6.             Windows.Storage.StorageFolder picturesFolder = Windows.Storage.KnownFolders.PicturesLibrary;
    7.  
    8.             var imageFile = await picturesFolder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);
    9.             var fs = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
    10.             var writer = new Windows.Storage.Streams.DataWriter(fs.GetOutputStreamAt(0));
    11.             writer.WriteBytes(byteArray);
    12.             await writer.StoreAsync();
    13.             writer.DetachStream();
    14.             await fs.FlushAsync();
    15.         }

    Hope that all came through fine. I'm currently on my phone :)
     
  4. mantler

    mantler

    Joined:
    Oct 30, 2013
    Posts:
    47
    I'm getting the same error message when I try Application.CaptureScreenshot... any news on this?
     
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,909
    You have to specify full path, where files can be saved, AppX/Data is readonly directory. Try using UnityEngine.Windows.Directory.tempraryfolder, and save screenshot there.
     
  6. mantler

    mantler

    Joined:
    Oct 30, 2013
    Posts:
    47
    Thanks! I'll give that a try tomorrow. :)

    Update: using that folder works, thanks!
     
    Last edited: Feb 21, 2014