Load image from file (non-resource) synchronously

Hi,

I need to load an image from the HDD. It has to look something like this:

public Texture2D GetImage(string id)
{
    // GET IMAGE PATH
    string path = GetImagePath(id);

    // LOAD IMAGE
    StartCoroutine("LoadImage", path);

    // RETURN IMAGE
    return image;
}

private IEnumerator LoadImage(string path)
{
    // Get image and wait for it to load
    WWW www = new WWW("file:///" + path.Replace('\\', '/'));
    yield return www;

    // Convert to texture
    image = new Texture2D(0, 0);
    www.LoadImageIntoTexture(image);
}

Obviously this doesn’t return the correct image since the coroutine is still busy when the return is called.

How can I load an image synchronously from a file path?

V1ncam

byte[] bytes = File.ReadAllBytes(path);

This worked. But because loading from the HDD was too slow I use another way. Pass on an EventHandler from the appropriate object and call that after the Coroutine.

You could use .NET to load the file as a byte array (I can’t remember the method but it’s probably inside of the File handling somewhere) then create a new Texture2D and call load on it passing in the byte array.