Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved ImageConversion.LoadImage always fails

Discussion in 'Scripting' started by hiineededaname, Aug 13, 2023.

  1. hiineededaname

    hiineededaname

    Joined:
    May 21, 2017
    Posts:
    2
    I'm trying to read a series of PNGs from a file, convert to byte arrays, then convert those to Texture2D. It seems like the byte array is being made correctly, but the function always returns false. I know the documentation says it returns false if the format is unsupported, but as far as I know I am using valid PNG files -- I even dropped them into Unity to check and they correctly rendered, and by checking the file explorer I can see that they are all the correct type.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.IO;
    3. using System.Linq;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. public class BatchImporter : EditorWindow
    8. {
    9.     string path = "";
    10.     string[] files = null;
    11.     string[] supportedImageExt = new string[] { "png", "jpg", "jpeg" };
    12.     List<Texture2D> images = new List<Texture2D>();
    13.  
    14.     [MenuItem("Tools/Batch Card Import")]
    15.     public static void BatchCardImport()
    16.     {
    17.         EditorWindow wnd = GetWindow<BatchImporter>();
    18.         wnd.titleContent = new GUIContent("Batch Importer");
    19.     }
    20.  
    21.     public void OnGUI()
    22.     {
    23.         if (GUILayout.Button("Select Card Folder"))
    24.         {
    25.             path = EditorUtility.OpenFolderPanel("Select Card Folder", "", "");
    26.             files = Directory.GetFiles(path);
    27.  
    28.             for (int i = 0; i < files.Length; i++)
    29.             {
    30.                 string file = files[i];
    31.                 string[] x = file.Split('.');
    32.                 string fileExt = x[x.Length - 1];
    33.                 if (supportedImageExt.Any(s => s.Equals(fileExt)))
    34.                 {
    35.                     byte[] fileData = File.ReadAllBytes(file);
    36.                     Debug.Log(fileData.Length);                                         // returns correct number of bytes
    37.                     Texture2D image = new Texture2D(2, 2);
    38.                     bool success = ImageConversion.LoadImage(image, fileData);
    39.                     Debug.Log(success);                                                 // always returns false
    40.                     images.Add(image);
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    You create a texture with 2x2 dimensions. Are you sure loading the image data will update the texture size? Try creating the texture with dimensions of the image you try to load.

    Also PNG have some issues if created by certain programs, Inkscape used to be one of them. To be perfectly sure try opening a PNG in PS or Paint.Nat and save it again as 32-bit PNG with or without alpha channel to see if that works. There is no guarantee that the editor uses the same loadimage method.
     
  3. hiineededaname

    hiineededaname

    Joined:
    May 21, 2017
    Posts:
    2
    It looks like this was the issue. I think something must have gotten corrupted when I downloaded the images because I got them off of a webpage. When I changed my file explorer to show icons instead of just filenames, the icon was empty. I saved the photos from a different source and the function worked just fine, but I feel a bit silly now. Thank you!
     
    Bunny83 likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    No need to feel silly. The first time I encountered this issue 10 years ago it took me a day to figure out that Inkscape was saving PNGs in a way that made them fail to load in the game engine (cocos2d).
     
    hiineededaname likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Some websites simply use the wrong file extension. So they may use png but it's actually a jpeg. Most browsers don't mind the extension and just try to load the content. However Unity relies on the extension. So it can't load a jpeg from a file with a png extension or the other way round. I've seen websites doing that a few times now.