/// <summary> /// Attempts to determine the ImageFormat of the source image. First attempts to parse the path, if a string is present in original.Tag. (or if 'original' is a string) /// Falls back to using original.RawFormat. Returns null if both 'original' is null. /// RawFormat has a bad reputation, so this may return unexpected values, like MemoryBitmap or something in some situations. /// </summary> /// <param name="original">The source image that was loaded from a stream, or a string path</param> /// <returns></returns> public static ImageFormat GetOriginalFormat(object original) { if (original == null) { return(null); } //Try to parse the original file extension first. string path = original as string; if (path == null && original is Image) { path = ((Image)original).Tag as string; } if (path == null && original is Image && ((Image)original).Tag is BitmapTag) { path = ((BitmapTag)((Image)original).Tag).Path; } //We have a path? Parse it! if (path != null) { ImageFormat f = DefaultEncoder.GetImageFormatFromPhysicalPath(path); if (f != null) { return(f); //From the path } } //Ok, I guess if there (a) wasn't a path, or (b), it didn't have a recognizable extension if (original is Image) { return(((Image)original).RawFormat); } return(null); }
/// <summary> /// Tries to parse an ImageFormat from the settings.Format value. /// If an unrecognized format is specified, returns null. /// If an unsupported format is specified, it is returned. /// If *no* format is specified, returns defaultValue. /// </summary> /// <param name="format"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static ImageFormat GetRequestedFormat(string format, ImageFormat defaultValue) { ImageFormat f = null; if (!string.IsNullOrEmpty(format)) { f = DefaultEncoder.GetImageFormatFromExtension(format); return(f); } //Fallback. No encoder was explicitly specified, so let's try to infer it from the image data. return(defaultValue); }