- Home /
How to load images from given folder?
Hi, I'd like the application to load all images from a given folder (a subfolder of the installation directory) at the beginning. The reason why I don't use assets is that it should be possible to exchange the images after installation. I've tried this piece of code so far in C#:
public ArrayList imageBuffer = new ArrayList();
private void LoadImages() { //load all Texture2D files in Resources/Images folder Object[] textures = Resources.LoadAll("Images"); for (int i=0; i < textures.Length; i++) imageBuffer.Add(textures[i]); }
Will the resources folder be copied to the installation directory during installation? Or is this not the right way if the contents of the image folder should be exchangeable after installation? How to do it right?
I added an answer (probably on 2nd page) if you don't want to use WWW and ins$$anonymous$$d prefer System.IO.
Answer by drhenry · Aug 21, 2010 at 11:37 AM
For all those, who'd like to load images (in my example "chessboard001.jpg",..., "chessboard027.jpg" from a given folder):
private void LoadImages() { string pathPrefix = @"file://"; string pathImageAssets = @"C:\HistoryCube_Assets\"; string pathSmall = @"small\"; string filename = @"chessboard"; string fileSuffix = @".jpg";
//create filename index suffix "001",...,"027" (could be "999" either)
for (int i=0; i < 27; i++)
{
string indexSuffix = "";
float logIdx = Mathf.Log10(i+1);
if (logIdx < 1.0)
indexSuffix += "00";
else if (logIdx < 2.0)
indexSuffix += "0";
indexSuffix += (i+1);
string fullFilename = pathPrefix + pathImageAssets + pathSmall + filename + indexSuffix + fileSuffix;
WWW www = new WWW(fullFilename);
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
//LoadImageIntoTexture compresses JPGs by DXT1 and PNGs by DXT5
www.LoadImageIntoTexture(texTmp);
imageBuffer.Add(texTmp);
}
where imageBuffer is of type ArrayList. This function blocks code execution until all images are loaded.
If you want to continue code execution while images are loading, call
StartCoroutine(LoadImages());
LoadImages must then be declared as:
private IEnumerator LoadImages() { ...
WWW www = new WWW(fullFilename);
yield return www;
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
//LoadImageIntoTexture compresses JPGs by DXT1 and PNGs by DXT5
www.LoadImageIntoTexture(texTmp);
imageBuffer.Add(texTmp);
... }
Hope it helps someone...
$$anonymous$$ark this answer as accepted, if it works for you, so it's bumped to the top. I just read through the previous two, and went off to investigate before seeing that you'd already solved it! :(
$$anonymous$$arking it as the accepted answer doesn't bump it upwards, sorry. I think it must be uprated by other users to climb upwards...
Noob question: Why do you use the character @ at string declarations? I couldn't find anything online due to the nature of the symbol...
I noticed the strings do not use escape characters if I put @ in front. Is that it...?
I found the answer, is the scape thing. https://stackoverflow.com/questions/17456686/cs1009-unrecognized-escape-sequence
I think it's "safe" string declaration. You see that nothing is escaped in them. Every normal string instantly must have only usual and escaped characters but this.
Answer by rdockterjr1 · Oct 02, 2013 at 02:35 PM
This will load all jpeg photos in a single folder as textures This uses the System.IO.Directory.GetFiles in C sharp
Hope this helps
public class PhotoViewer : MonoBehaviour {
GameObject[] gameObj;
Texture2D[] textList;
string[] files;
string pathPreFix;
// Use this for initialization
void Start () {
//Change this to change pictures folder
string path = @"C:\Users\Public\Pictures\Sample Pictures\";
pathPreFix = @"file://";
files = System.IO.Directory.GetFiles(path, "*.jpg");
gameObj= GameObject.FindGameObjectsWithTag("Pics");
StartCoroutine(LoadImages());
}
void Update () {
}
private IEnumerator LoadImages(){
//load all images in default folder as textures and apply dynamically to plane game objects.
//6 pictures per page
textList = new Texture2D[files.Length];
int dummy = 0;
foreach(string tstring in files){
string pathTemp = pathPreFix + tstring;
WWW www = new WWW(pathTemp);
yield return www;
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(texTmp);
textList[dummy] = texTmp;
gameObj[dummy].renderer.material.SetTexture("_MainTex", texTmp);
dummy++;
}
}
}
Thanks! Exactly what i was looking for and easy to use!
Answer by drhenry · Aug 20, 2010 at 03:43 PM
Sorry, I just didn't want people to forget about my issue, so I bumped ;) And this time I need the "code snippet" functionality, which isn't available in comments...
I don't get around this www stuff...and how to wait at program start for the image to be loaded completely...Can anyone explain it using C#?
In Start() I call LoadImages() which shall load all images in a given folder (C:\Assets\Images). But even if I write just
private IEnumerator LoadImages()
{
print("yeah!");
print("image loaded");
yield return 0;
}
nothing is ever printed to the console. Why?
Ah, in Start I have to call StartCoRoutine(LoadImages)... But this will continue code execution although not all images might have been loaded yet. How can I wait in Start until LoadImages is finished?
use Debug.Log() ins$$anonymous$$d of print(), also make sure you call the coroutine using StartCoroutine()
sigh sometimes I'm so dumb...of course without the yield statement (as in all Unity examples) I won't need a coroutine anymore. That's it! Thanks.
Answer by Kalu · Feb 13, 2012 at 11:00 AM
dont know if it could be help someone but in C# =>
if(cur_image_loaded != null){
GUI.DrawTexture(new Rect(vignette_pos_x-203, vignette_pos_y-30, 256f, 256f), cur_image_loaded, ScaleMode.ScaleToFit, true);
}
StartCoroutine(load_image_preview(image_to_load));
private IEnumerator load_image_preview(string _path)
{
WWW www = new WWW(_path);
yield return www;
Texture2D texTmp = new Texture2D(256, 256, TextureFormat.RGB24, false);
www.LoadImageIntoTexture(texTmp);
cur_image_loaded = new Texture2D(256, 256, TextureFormat.RGB24, false);
cur_image_loaded = texTmp;
}
Answer by Amelia · Apr 25, 2013 at 09:18 AM
Folllowing is my sample code in image loading from a file folder in c#, the code work very well.
using System;
using RasterEdge.Imaging.Basic.Core;
using RasterEdge.Imaging.Basic;
public static REImage OpenImageFile(string filePath);
public static void LoadImageFromFileDemo()
{
string fileName = "c:/Sample.png";
REImage reImage = REFile.OpenImageFile(fileName);
}
Um, I guess we'd need to know what RasterEdge was though?