Search Unity

Convert RawImage to Texture2d

Discussion in 'UGUI & TextMesh Pro' started by domdev, Sep 4, 2019.

  1. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    since I cant find a way to make webcamtexture rotate to portrait when I make it as texture2d, I want to use rawimage to make texture2d.. is it possible?

    I tried to use this code
    Code (CSharp):
    1. Texture2D frameTexture = (Texture2D) frame.mainTexture;
    2. Texture2D texture = new Texture2D(frameTexture.width, frameTexture.height);
    3. texture.SetPixels(frameTexture.GetPixels());
    4. texture.Apply();
    but I getting "InvalidCastException: Specified cast is not valid."
     
  2. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
  3. Lokesh2022

    Lokesh2022

    Joined:
    May 8, 2021
    Posts:
    9
    public RawImage profileImage;
    private Texture2D textureImg;
     
  4. Lokesh2022

    Lokesh2022

    Joined:
    May 8, 2021
    Posts:
    9
    convert raw image into texture

    public RawImage profileImage;
    private Texture2D textureImg;

    textureImg = profileImage.texture as Texture2D;

    WWWForm form = new WWWForm();

    byte[] imgByte = textureImg.EncodeToPNG();

    Debug.Log(profileImage);
    form.AddBinaryData("myimage", imgByte, "imageFromUnity.png", "image/png");