Search Unity

Walshed-off image when converting byte[] image to tex2d/sprite

Discussion in 'General Graphics' started by Tarrag, Jan 27, 2020.

  1. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    Hello
    When a user uploads an image (png or jpg) on my game I convert it at runtime as per below to a sprite but it renders washed off, particularly noticeable for blacks and whites.

    Original image:
    witcher.jpg
    Converted image: convertedWitcher.jpg

    I develop on URP but I've tested the pipeline and it's not the problem - I manually assigned the image as Sprite (2D and UI) in the inspector to the UI and it looks fine.

    This is the code I use:
    Code (CSharp):
    1. private void FileWasOpenedEventHandler(byte[] data)
    2.     {
    3. string path = UnityEditor.EditorUtility.OpenFilePanelWithFilters(title,localImagePath,filters);
    4. byte[] fileContent = File.ReadAllBytes(path);
    5.  
    6. //bytes to texture2D
    7. Texture2D thisTex2d = GetTexture2D(data, name);//placeholder rawimage component
    8.  
    9. //create sprite to attach to image component
    10. Sprite thisSprite = Sprite.Create(thisTex2D, new Rect(0.0f, 0.0f, RGBTexture2d.width, RGBTexture2d.height), Vector2.one);
    11.  
    12. Hero.GetComponent<Image>().sprite = thisPropSprite;
    13. }
    14.  
    15. public Texture2D GetTexture2D(byte[] data, string name)
    16.         {
    17.             var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, true);
    18.             texture.LoadImage(data);
    19.             texture.Apply();
    20.             return texture;
    21.         }
    22.  
    Can someone please shed some light as to what I may be doing wrong or considerations?

    Thanks a bunch
     
    Last edited: Jan 27, 2020
  2. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    Please ignore, if it helps anyone TextureFormat was the culprit :eek:
    var texture = new Texture2D(2, 2, TextureFormat.BGRA32, false);