Question by
KnightRiderGuy · Aug 18, 2017 at 01:27 AM ·
c#uiimagesimage loaderindexing-array
Trouble With Image Loading Script
Can't see what I'm messing up here. I'm trying to get this so I can load images that have been taken by the web camera to load into a UI texture where I can use some buttons to call up the images one at a time to preview them.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine.UI;
public class LoadImage : MonoBehaviour
{
[SerializeField] [HideInInspector] private int currentIndex = 0;
public enum SeekDirection { Forward, Backward }
private FileInfo[] SCFiles;
private List<string> validExtensions = new List<string> { ".png", ".jpg" };
private string filePath = "/Users/michaelknight/Desktop/Surveillance/CamCapture/";
//string filePath;
public byte[] fileData; // load data inside a byte array 0x89,0x50,0x4E,0x47,0x0D...
public Image imageToDisplay; // Assign in Inspector the UI Image
// Use this for initialization
void Start () {
if (Application.isEditor)
filePath = "Assets/Surveillance/CamCapture";
// Load images
// get all valid files
var info = new DirectoryInfo (filePath);
SCFiles = info.GetFiles ()
.Where (f => IsValidFileType (f.Name))
.ToArray ();
/*filePath = "/Users/michaelknight/Desktop/Surveillance/CamCapture/5.png"; // the path of the image
fileData = File.ReadAllBytes(filePath); // 1.read the bytes array
Texture2D tex = new Texture2D(2, 2); // 2.create a texture named tex
tex.LoadImage(fileData); // 3.load inside tx the bytes and use the correct image size
Rect rec = new Rect(0, 0, tex.width, tex.height); // 4.create a rect using the textute dimensions
Sprite spriteToUse = Sprite.Create(tex,rec,new Vector2(0.5f,0.5f),100); //5. convert the texture in sprite
imageToDisplay.sprite = spriteToUse; //6.load the sprite used by UI Image
*/
}
public void Update()
{
StartCoroutine(LoadFile(SCFiles[currentIndex].FullName));
}
void Seek(SeekDirection d)
{
if (d == SeekDirection.Forward)
currentIndex = (currentIndex + 1) % SCFiles.Length;
else {
currentIndex--;
if (currentIndex < 0) currentIndex = SCFiles.Length -1;
}
}
public void NextSCimage (){
Seek (SeekDirection.Backward);
StartCoroutine (LoadFile (SCFiles [currentIndex].FullName));
}
bool IsValidFileType(string fileName)
{
return validExtensions.Contains(Path.GetExtension(fileName));
}
IEnumerator LoadFile(string path){
yield return new WaitForSeconds (0.5f);
Debug.Log ("Loading First Image");
filePath = ("file://" + path);
//filePath = "/Users/michaelknight/Desktop/Surveillance/CamCapture/5.png"; // the path of the image
fileData = File.ReadAllBytes(filePath); // 1.read the bytes array
Texture2D tex = new Texture2D(2, 2); // 2.create a texture named tex
tex.LoadImage(fileData); // 3.load inside tx the bytes and use the correct image size
Rect rec = new Rect(0, 0, tex.width, tex.height); // 4.create a rect using the textute dimensions
Sprite spriteToUse = Sprite.Create(tex,rec,new Vector2(0.5f,0.5f),100); //5. convert the texture in sprite
imageToDisplay.sprite = spriteToUse; //6.load the sprite used by UI Image
}
}
I have a simple UI button et up where i'm trying to access the next image to load. but I get this error message:
DirectoryNotFoundException: Could not find a part of the path "/Users/michaelknight/Desktop/Load External Image/file:/Users/michaelknight/Desktop/Load External Image/Assets/Surveillance/CamCapture/2.png".
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:292)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.File.OpenRead (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363)
System.IO.File.ReadAllBytes (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:538)
LoadImage+<LoadFile>c__Iterator0.MoveNext () (at Assets/LoadImage.cs:93)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Comment
Best Answer
Answer by KnightRiderGuy · Aug 18, 2017 at 09:03 PM
Figured it out. I needed to change this
fileData = File.ReadAllBytes(filePath);
to This:
fileData = File.ReadAllBytes(path);