string GetUri(object imageSource) { if (imageSource == null) { return(null); } string uri = imageSource as string; if (uri != null) { return(uri); } ImageSource convertedSource; if (!ImageSourceFactory.TryCreateImageSource(imageSource, 0, 0, out convertedSource)) { return(null); } AbstractProperty uriProperty; if (TryGetUriProperty(convertedSource, out uriProperty)) { return(uriProperty.GetValue() as string); } ServiceRegistration.Get <ILogger>().Warn("ImageSourceWrapper: Unsupported ImageSource type '{0}'", imageSource.GetType()); return(null); }
/// <summary> /// Loads an ImageSource and allows control of thumbnail use. /// Morpheus_xx, 2011-12-13: For fallback sources no thumbnails should be used, because ALL thumbs are created as JPG. This currenly causes an issue: /// Source -> no thumbnail created -> FallbackSource (PNG) -> creates a JPG thumbnail, so Alpha-Channel of FallbackSource is lost. /// TODO: Image class and thumbnail handling should be refactored to allow more control about image formats and thumbs usage. /// </summary> /// <param name="source">Source</param> /// <param name="allowThumbs">True to allow building a thumbnail of given source</param> /// <returns>ImageSource or null</returns> protected ImageSource LoadImageSource(object source, bool allowThumbs) { if (source == null) { return(null); } bool thumbnail = allowThumbs && Thumbnail; ImageSource imageSource; if (ImageSourceFactory.TryCreateImageSource(source, (int)Width, (int)Height, out imageSource)) { return(imageSource); } string uriSource = source as string; if (!string.IsNullOrEmpty(uriSource)) { // Remember to adapt list of supported extensions for image player plugin... if (IsValidSource(uriSource)) { BitmapImageSource bmi = new BitmapImageSource { UriSource = uriSource, Thumbnail = thumbnail }; if (thumbnail) { // Set the requested thumbnail dimension, to use the best matching format. // Note: Math.Max returns NaN if one argument is NaN (which casts to int.MinValue), so the additional Max with 0 catches this bmi.ThumbnailDimension = Math.Max((int)Math.Max(Width, Height), 0); } return(bmi); } // TODO: More image types } string warnSource = source.ToString(); if (_formerWarnURI != warnSource) { if (!string.IsNullOrEmpty(warnSource)) { ServiceRegistration.Get <ILogger>().Warn("Image: Image source '{0}' is not supported", warnSource); } // Remember if we already wrote a warning to the log to avoid log flooding _formerWarnURI = warnSource; } return(null); }