/// <summary>
/// Loads an image from a resource
/// </summary>
/// <returns>An image</returns>
/// <param name="assembly">The assembly from which to load the image</param>
/// <param name="resource">Resource name</param>
/// <remarks>
/// This method will look for alternative versions of the image with different resolutions.
/// For example, if a resource is named "foo.png", this method will load
/// other resources with the name "*****@*****.**", where XXX can be any arbitrary string. For example "*****@*****.**".
/// Each of those resources will be considered different versions of the same image.
/// </remarks>
public static Image FromResource(Assembly assembly, string resource)
{
if (assembly == null)
throw new ArgumentNullException ("assembly");
if (resource == null)
throw new ArgumentNullException ("resource");
var toolkit = Toolkit.CurrentEngine;
if (toolkit == null)
throw new ToolkitNotInitializedException ();
var name = Path.GetFileNameWithoutExtension (resource);
var img = toolkit.ImageBackendHandler.LoadFromResource (assembly, resource);
if (img == null)
throw new InvalidOperationException ("Resource not found: " + resource);
var reqSize = toolkit.ImageBackendHandler.GetSize (img);
var ext = GetExtension (resource);
var altImages = new List<Tuple<string,object>> ();
foreach (var r in assembly.GetManifestResourceNames ()) {
int i = r.LastIndexOf ('@');
if (i != -1) {
string rname = r.Substring (0, i);
if (rname == resource || rname == name) {
var rim = toolkit.ImageBackendHandler.LoadFromResource (assembly, r);
if (rim != null)
altImages.Add (new Tuple<string, object> (r, rim));
}
}
}
if (altImages.Count > 0) {
altImages.Insert (0, new Tuple<string, object> (resource, img));
if (ext == ".9.png")
return CreateComposedNinePatch (toolkit, altImages);
img = toolkit.ImageBackendHandler.CreateMultiResolutionImage (altImages.Select (i => i.Item2));
}
var res = new Image (img, toolkit) {
requestedSize = reqSize
};
res.NativeRef.SetResourceSource (assembly, resource);
if (ext == ".9.png")
res = new NinePatchImage (res.ToBitmap ());
return res;
}