GetResource() public method

Gets the content of a resource
The add-in engine will look for resources in the main add-in assembly and in all included add-in assemblies.
public GetResource ( string resourceName ) : Stream
resourceName string /// Name of the resource ///
return Stream
Esempio n. 1
0
		static string InternalGetStockIdFromResource (RuntimeAddin addin, string id, Gtk.IconSize size)
		{
			if (!id.StartsWith ("res:", StringComparison.Ordinal))
				return id;

			id = id.Substring (4);
			int addinId = GetAddinId (addin);
			Dictionary<string, string> hash = addinIcons[addinId];
			string stockId = "__asm" + addinId + "__" + id + "__" + size;
			if (!hash.ContainsKey (stockId)) {
				System.IO.Stream stream = addin.GetResource (id);
				if (stream != null) {
					Gdk.Pixbuf pix1, pix2 = null;
					using (stream) {
						pix1 = new Gdk.Pixbuf (stream);
					}
					stream = addin.GetResource2x (id);
					if (stream != null) {
						using (stream) {
							pix2 = new Gdk.Pixbuf (stream);
						}
					}
					AddToIconFactory (stockId, pix1, pix2, size);
				}
				hash[stockId] = stockId;
			}
			return stockId;
		}
		static Xwt.Drawing.Image LoadStockIcon (RuntimeAddin addin, string stockId, string resource, string imageFile, string iconId, Gtk.IconSize iconSize, string animation, bool forceWildcard)
		{
			try {
				Gdk.Pixbuf pixbuf = null, pixbuf2x = null;
				AnimatedIcon animatedIcon = null;
				Func<Stream[]> imageLoader = null;

				if (!string.IsNullOrEmpty (resource) || !string.IsNullOrEmpty (imageFile)) {
					// using the stream directly produces a gdk warning.
					byte[] buffer;

					if (resource != null) {
						imageLoader = delegate {
							var stream = addin.GetResource (resource);
							var stream2x = addin.GetResource2x (resource);
							if (stream2x == null)
								return new [] { stream };
							else
								return new [] { stream, stream2x };
						};
					}
					else {
						imageLoader = delegate {
							var file = addin.GetFilePath (imageFile);
							var stream = File.OpenRead (file);
							Stream stream2x = null;
							var file2x = Path.Combine (Path.GetDirectoryName (file), Path.GetFileNameWithoutExtension (file) + "@2x" + Path.GetExtension (file));
							if (File.Exists (file2x))
								stream2x = File.OpenRead (file2x);
							else {
								file2x = file + "@2x";
								if (File.Exists (file2x))
									stream2x = File.OpenRead (file2x);
							}
							if (stream2x == null)
								return new [] { stream };
							else
								return new [] { stream, stream2x };
						};
					}
					var streams = imageLoader ();

					var st = streams[0];
					var st2x = streams.Length > 1 ? streams[1] : null;

					using (st) {
						if (st == null || st.Length < 0) {
							LoggingService.LogError ("Did not find resource '{0}' in addin '{1}' for icon '{2}'",
								resource, addin.Id, stockId);
							return null;
						}
						buffer = new byte [st.Length];
						st.Read (buffer, 0, (int)st.Length);
					}
					pixbuf = new Gdk.Pixbuf (buffer);

					using (st2x) {
						if (st2x != null && st2x.Length >= 0) {
							buffer = new byte [st2x.Length];
							st2x.Read (buffer, 0, (int)st2x.Length);
							pixbuf2x = new Gdk.Pixbuf (buffer);
						}
					}

				} else if (!string.IsNullOrEmpty (iconId)) {
					var id = GetStockIdForImageSpec (addin, iconId, iconSize);
					pixbuf = GetPixbuf (id, iconSize);
					pixbuf2x = Get2xIconVariant (pixbuf);
					// This may be an animation, get it
					animationFactory.TryGetValue (id, out animatedIcon);
				} else if (!string.IsNullOrEmpty (animation)) {
					string id = GetStockIdForImageSpec (addin, "animation:" + animation, iconSize);
					pixbuf = GetPixbuf (id, iconSize);
					// This *should* be an animation
					animationFactory.TryGetValue (id, out animatedIcon);
				}

				Gtk.IconSize size = forceWildcard? Gtk.IconSize.Invalid : iconSize;
				if (pixbuf != null)
					AddToIconFactory (stockId, pixbuf, pixbuf2x, size);

				if (animatedIcon != null)
					AddToAnimatedIconFactory (stockId, animatedIcon);

				var img = Xwt.Toolkit.CurrentEngine.WrapImage (pixbuf);
				if (pixbuf2x != null) {
					var img2x = Xwt.Toolkit.CurrentEngine.WrapImage (pixbuf2x);
					img = Xwt.Drawing.Image.CreateMultiResolutionImage (new [] { img, img2x });
				}
				if (imageLoader != null)
					img.SetStreamSource (imageLoader);

				return img;

			} catch (Exception ex) {
				LoggingService.LogError (string.Format ("Error loading icon '{0}'", stockId), ex);
				return null;
			}
		}