Need to save UI.Image to png

Hi all,

I have ui.Image1 with Mask component and ui.Image2 that is placed inside ui.Image1, then I want to save the result to Texture2d to save the resulting image to png.

Sprite sprite = sourceImage.sprite;
Texture2D newText = new Texture2D((int)sprite.rect.width,(int)sprite.rect.height);
Color[] newColors = sprite.texture.GetPixels((int)sprite.textureRect.x,
                                             (int)sprite.textureRect.y,
                                             (int)sprite.textureRect.width,
                                             (int)sprite.textureRect.height );
newText.SetPixels(newColors);
newText.Apply();

System.IO.File.WriteAllBytes(_SavePath + "photo.png", newText.EncodeToPNG());

but I have the error:

If I change the texture type of ‘mask’ from Sprite to Advanced - the mask stop work. Let me know what need to do.

it’s a good question…