static Image CreateComposedNinePatch(Toolkit toolkit, IEnumerable <Tuple <string, ImageTagSet, bool, object> > altImages) { var npImage = new NinePatchImage(); foreach (var fi in altImages) { int i = fi.Item1.LastIndexOf('@'); double scaleFactor; if (i == -1) { scaleFactor = 1; } else { int j = fi.Item1.IndexOf('x', ++i); if (!double.TryParse(fi.Item1.Substring(i, j - i), out scaleFactor)) { toolkit.ImageBackendHandler.Dispose(fi.Item4); continue; } } npImage.AddFrame(new Image(fi.Item4, toolkit).ToBitmap(), scaleFactor); } return(npImage); }
public static Image FromFile(string file) { var toolkit = Toolkit.CurrentEngine; if (toolkit == null) { throw new ToolkitNotInitializedException(); } var ext = GetExtension(file); var img = toolkit.ImageBackendHandler.LoadFromFile(file); List <Tuple <string, object> > altImages = null; foreach (var s in supportedScales) { var fn = file.Substring(0, file.Length - ext.Length) + "@" + s + ext; if (File.Exists(fn)) { if (altImages == null) { altImages = new List <Tuple <string, object> > (); altImages.Add(new Tuple <string, object> (file, img)); } altImages.Add(new Tuple <string, object> (fn, toolkit.ImageBackendHandler.LoadFromFile(fn))); } } if (altImages != null) { if (ext == ".9.png") { return(CreateComposedNinePatch(toolkit, altImages)); } img = toolkit.ImageBackendHandler.CreateMultiResolutionImage(altImages.Select(i => i.Item2)); } var res = new Image(img, toolkit); if (ext == ".9.png") { res = new NinePatchImage(res.ToBitmap()); } return(res); }
static Image LoadImage(ImageLoader loader, string fileName, ImageTagSet tagFilter) { var toolkit = Toolkit.CurrentEngine; if (toolkit == null) { throw new ToolkitNotInitializedException(); } var img = loader.LoadImage(fileName); var reqSize = toolkit.ImageBackendHandler.GetSize(img); var ext = GetExtension(fileName); var name = fileName.Substring(0, fileName.Length - ext.Length); var altImages = new List <Tuple <string, ImageTagSet, bool, object> > (); var tags = Context.RegisteredStyles; foreach (var r in loader.GetAlternativeFiles(fileName, name, ext)) { int scale; ImageTagSet fileTags; if (ParseImageHints(name, r, ext, out scale, out fileTags) && (tagFilter == null || tagFilter.Equals(fileTags))) { var rim = loader.LoadImage(r); if (rim != null) { altImages.Add(new Tuple <string, ImageTagSet, bool, object> (r, fileTags, scale > 1, rim)); } } } if (altImages.Count > 0) { altImages.Insert(0, new Tuple <string, ImageTagSet, bool, object> (fileName, ImageTagSet.Empty, false, img)); var list = new List <Tuple <Image, string[]> > (); foreach (var imageGroup in altImages.GroupBy(t => t.Item2)) { Image altImg; if (ext == ".9.png") { altImg = CreateComposedNinePatch(toolkit, imageGroup); } else { var ib = toolkit.ImageBackendHandler.CreateMultiResolutionImage(imageGroup.Select(i => i.Item4)); altImg = loader.WrapImage(fileName, imageGroup.Key, ib, reqSize); } list.Add(new Tuple <Image, string[]> (altImg, imageGroup.Key.AsArray)); } if (list.Count == 1) { return(list [0].Item1); } else { return(new ThemedImage(list, reqSize)); } } else { var res = loader.WrapImage(fileName, ImageTagSet.Empty, img, reqSize); if (ext == ".9.png") { res = new NinePatchImage(res.ToBitmap()); } return(res); } }
static Image CreateComposedNinePatch(Toolkit toolkit, List<Tuple<string,object>> altImages) { var npImage = new NinePatchImage (); foreach (var fi in altImages) { int i = fi.Item1.LastIndexOf ('@'); double scaleFactor; if (i == -1) scaleFactor = 1; else { int j = fi.Item1.IndexOf ('x', ++i); if (!double.TryParse (fi.Item1.Substring (i, j - i), out scaleFactor)) { toolkit.ImageBackendHandler.Dispose (fi.Item2); continue; } } npImage.AddFrame (new Image (fi.Item2, toolkit).ToBitmap (), scaleFactor); } return npImage; }
/// <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; }
public static Image FromFile(string file) { var toolkit = Toolkit.CurrentEngine; if (toolkit == null) throw new ToolkitNotInitializedException (); var ext = GetExtension (file); var img = toolkit.ImageBackendHandler.LoadFromFile (file); List<Tuple<string,object>> altImages = null; foreach (var s in supportedScales) { var fn = file.Substring (0, file.Length - ext.Length) + "@" + s + "x" + ext; if (File.Exists (fn)) { if (altImages == null) { altImages = new List<Tuple<string, object>> (); altImages.Add (new Tuple<string, object> (file, img)); } altImages.Add (new Tuple<string, object> (fn, toolkit.ImageBackendHandler.LoadFromFile (fn))); } } if (altImages != null) { if (ext == ".9.png") return CreateComposedNinePatch (toolkit, altImages); img = toolkit.ImageBackendHandler.CreateMultiResolutionImage (altImages.Select (i => i.Item2)); } var res = new Image (img, toolkit); if (ext == ".9.png") res = new NinePatchImage (res.ToBitmap ()); return res; }
/// <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); }
static Image LoadImage(ImageLoader loader, string fileName, ImageTagSet tagFilter) { var toolkit = Toolkit.CurrentEngine; if (toolkit == null) throw new ToolkitNotInitializedException (); var img = loader.LoadImage (fileName); var reqSize = toolkit.ImageBackendHandler.GetSize (img); var ext = GetExtension (fileName); var name = fileName.Substring (0, fileName.Length - ext.Length); var altImages = new List<Tuple<string,ImageTagSet,bool,object>> (); var tags = Context.RegisteredStyles; foreach (var r in loader.GetAlternativeFiles (fileName, name, ext)) { int scale; ImageTagSet fileTags; if (ParseImageHints (name, r, ext, out scale, out fileTags) && (tagFilter == null || tagFilter.Equals (fileTags))) { var rim = loader.LoadImage (r); if (rim != null) altImages.Add (new Tuple<string, ImageTagSet, bool, object> (r, fileTags, scale > 1, rim)); } } if (altImages.Count > 0) { altImages.Insert (0, new Tuple<string, ImageTagSet, bool, object> (fileName, ImageTagSet.Empty, false, img)); var list = new List<Tuple<Image,string[]>> (); foreach (var imageGroup in altImages.GroupBy (t => t.Item2)) { Image altImg; if (ext == ".9.png") altImg = CreateComposedNinePatch (toolkit, imageGroup); else { var ib = toolkit.ImageBackendHandler.CreateMultiResolutionImage (imageGroup.Select (i => i.Item4)); altImg = loader.WrapImage (fileName, imageGroup.Key, ib, reqSize); } list.Add (new Tuple<Image,string[]> (altImg, imageGroup.Key.AsArray)); } if (list.Count == 1) return list [0].Item1; else { return new ThemedImage (list, reqSize); } } else { var res = loader.WrapImage (fileName, ImageTagSet.Empty, img, reqSize); if (ext == ".9.png") res = new NinePatchImage (res.ToBitmap ()); return res; } }