Axiom.Media.Image.FromFile C# (CSharp) Method

FromFile() public static method

Loads an image file from the file system.
public static FromFile ( string fileName ) : Image
fileName string Full path to the image file on disk.
return Image
		public static Image FromFile( string fileName )
		{
			Contract.RequiresNotEmpty( fileName, "fileName" );

			int pos = fileName.LastIndexOf( "." );

			if ( pos == -1 )
			{
				throw new AxiomException( "Unable to load image file '{0}' due to missing extension.", fileName );
			}

			// grab the extension from the filename
			string ext = fileName.Substring( pos + 1, fileName.Length - pos - 1 );

			// find a registered codec for this type
			ICodec codec = CodecManager.Instance.GetCodec( ext );

			Stream encoded = ResourceGroupManager.Instance.OpenResource( fileName );
			if ( encoded == null )
			{
				throw new FileNotFoundException( fileName );
			}

			// decode the image data
			MemoryStream decoded = new MemoryStream();
			ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode( encoded, decoded );
			encoded.Close();

			Image image = new Image();

			// copy the image data
			image.height = data.height;
			image.width = data.width;
			image.depth = data.depth;
			image.format = data.format;
			image.flags = data.flags;
			image.numMipMaps = data.numMipMaps;

			// stuff the image data into an array
			byte[] buffer = new byte[ decoded.Length ];
			decoded.Position = 0;
			decoded.Read( buffer, 0, buffer.Length );
			decoded.Close();

			image.SetBuffer( buffer );

			return image;
		}