# Loading an image from Disk to used to replace a source image for an image object

I am working on a Unity project where I need to be able to grab an image from disk and then have it be replaced as the source image for an image gameobject. Currently I already have a filebrowser implemented that gives me the filepath.

if (File.Exists(output)){
GameOjbect imageselect = GameObject.Find("ImageSelection");
imageselect.GetComponent<Image>();
//I need to make the file location at path output be the sprite?/sourceimage? for imageselect
}


Sorry if this has already been asked, I have been looking at other examples this morning with no luck.

You can use Texture2D.LoadImage to load your image into a Texture2D, assuming it's a PNG or JPG image.

The method takes in a byte[], so you'll have to use File.ReadAllBytes(output).

Something like this:

if (File.Exists(output))
{
var texture2D = new Texture2D(2, 2);

{
_image.sprite = Sprite.Create(texture2D,
new Rect(0, 0, texture2D.width, texture2D.height),
new Vector2(0.5f, 0.5f));
}
else
{

The Texture2D.LoadImage() method will automatically resize the 2x2 texture2D object depending on the file loaded.
There's also an optional fourth parameter for Sprite.Create() that takes in a float pixelsPerUnit.
• You can also use the WWW class to asynchronously load an image from a local or network location. You can then access the image as a Texture2D by reading the .texture property of your www object. – DMGregory Dec 14 '16 at 16:26