Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Why my scene freeze a second while saving texture2D as a .png image in the disk?

Does any one know why the scene will freeze in this situation?

I used Locatable camera in Unity to capture a photo and then show it in a Gameobject like so:

`private static Texture2D targetTexture;`
    public void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            // Create our Texture2D for use and set the correct resolution
            Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
            targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);
            //// Do as we wish with the texture such as apply it to a material, etc.

            MeasureManager.Instance.PopupWindow.SetActive(true);

            // should change position later
            //popupWindow.transform.position.Set(0, 0, 1);
            var photoBody = GameObject.Find("PopWindowBody");
            if (photoBody != null)
            {
                photoBody.GetComponent<Renderer>().material.mainTexture = targetTexture;
            }
        }
        photoCapture.StopPhotoModeAsync(OnStoppedPhotoMode);
    }

Then when I want to save this photo, I just convert the texture2D object to png and then save it like this:

        public static void SavePicture()
        {
            var bytes = targetTexture.EncodeToPNG();
            var filename = "xxxxxx.png";
            var filePath = Path.Combine(Application.persistentDataPath, filename);
    #if UNITY_EDITOR || !UNITY_WSA
                new System.Threading.Thread
    #else
            System.Threading.Tasks.Task.Run
    #endif
                (() =>
                {
                    using (var file = File.Open(filePath, FileMode.Create))
                    {
                        var binary = new BinaryWriter(file);
                        binary.Write(bytes);
                    }
                })

    #if UNITY_EDITOR || !UNITY_WSA
                .Start()
    #endif
                ;
        }

When I click my save button, the scene will freeze for 1-2 seconds, would someone help me to solve this problem? I don't understand why it would stuck, there's nothing related with UI in this code, I was thinking if the problem is possible with the code var bytes = targetTexture.EncodeToPNG(); in main thread, but as the compiler suggested, this EncodeToPNG method must be executed in the main thread.

Thanks a lot if anyone can solve this issue.

Tagged:
Sign In or Register to comment.