ImageConversion.LoadImage always fails

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.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class BatchImporter : EditorWindow
{
    string path = "";
    string[] files = null;
    string[] supportedImageExt = new string[] { "png", "jpg", "jpeg" };
    List<Texture2D> images = new List<Texture2D>();

    [MenuItem("Tools/Batch Card Import")]
    public static void BatchCardImport()
    {
        EditorWindow wnd = GetWindow<BatchImporter>();
        wnd.titleContent = new GUIContent("Batch Importer");
    }

    public void OnGUI()
    {
        if (GUILayout.Button("Select Card Folder"))
        {
            path = EditorUtility.OpenFolderPanel("Select Card Folder", "", "");
            files = Directory.GetFiles(path);

            for (int i = 0; i < files.Length; i++)
            {
                string file = files[i];
                string[] x = file.Split('.');
                string fileExt = x[x.Length - 1];
                if (supportedImageExt.Any(s => s.Equals(fileExt)))
                {
                    byte[] fileData = File.ReadAllBytes(file);
                    Debug.Log(fileData.Length);                                         // returns correct number of bytes
                    Texture2D image = new Texture2D(2, 2);
                    bool success = ImageConversion.LoadImage(image, fileData);
                    Debug.Log(success);                                                 // always returns false
                    images.Add(image);
                }
            }
        }
    }
}

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.

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!

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).

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.