private static BitmapDecoder GetDecoder(BitmapSource image, out GifFile gifFile) { gifFile = null; BitmapDecoder decoder = null; Stream bitmapStream = null; Uri result = null; BitmapCreateOptions none = BitmapCreateOptions.None; BitmapImage image2 = image as BitmapImage; if (image2 == null) { BitmapFrame frame = image as BitmapFrame; if (frame != null) { decoder = frame.Decoder; Uri.TryCreate(frame.BaseUri, frame.ToString(), out result); } } else { none = image2.CreateOptions; if (image2.StreamSource != null) { bitmapStream = image2.StreamSource; } else if (image2.UriSource != null) { result = image2.UriSource; if ((image2.BaseUri != null) && !result.IsAbsoluteUri) { result = new Uri(image2.BaseUri, result); } } } if (decoder == null) { if (bitmapStream != null) { bitmapStream.Position = 0L; decoder = BitmapDecoder.Create(bitmapStream, none, BitmapCacheOption.OnLoad); } else if ((result != null) && result.IsAbsoluteUri) { decoder = BitmapDecoder.Create(result, none, BitmapCacheOption.OnLoad); } } if ((decoder is GifBitmapDecoder) && !CanReadNativeMetadata(decoder)) { if (bitmapStream != null) { bitmapStream.Position = 0L; gifFile = GifFile.ReadGifFile(bitmapStream, true); } else if (result != null) { gifFile = DecodeGifFile(result); } } return(decoder); }
private static GifFile DecodeGifFile(Uri uri) { Stream stream = null; if (uri.Scheme == PackUriHelper.UriSchemePack) { StreamResourceInfo sri; if (uri.Authority == "siteoforigin:,,,") { sri = Application.GetRemoteStream(uri); } else { sri = Application.GetResourceStream(uri); } if (sri != null) { stream = sri.Stream; } } else { WebClient wc = new WebClient(); stream = wc.OpenRead(uri); } if (stream != null) { using (stream) { return(GifFile.ReadGifFile(stream, true)); } } return(null); }
private static GifFile DecodeGifFile(Uri uri) { Stream stream = null; if (uri.Scheme != PackUriHelper.UriSchemePack) { stream = new WebClient().OpenRead(uri); } else { StreamResourceInfo info = (uri.Authority != "siteoforigin:,,,") ? Application.GetResourceStream(uri) : Application.GetRemoteStream(uri); if (info != null) { stream = info.Stream; } } if (stream == null) { return(null); } using (stream) { return(GifFile.ReadGifFile(stream, true)); } }
private static GifFile?DecodeGifFile(Uri uri) { Stream?stream = null; if (uri.Scheme == PackUriHelper.UriSchemePack) { var sri = uri.Authority == "siteoforigin:,,," ? Application.GetRemoteStream(uri) : Application.GetResourceStream(uri); if (sri != null) { stream = sri.Stream; } } else { var wc = new WebClient(); stream = wc.OpenRead(uri); } if (stream == null) { return(null); } using (stream) return(GifFile.ReadGifFile(stream, true)); }
private static GifFile?DecodeGifFile(Uri uri) { Stream?stream = null; if (uri.Scheme == PackUriHelper.UriSchemePack) { StreamResourceInfo sri; if (uri.Authority == "siteoforigin:,,,") { sri = Application.GetRemoteStream(uri); } else { sri = Application.GetResourceStream(uri); } if (sri != null) { stream = sri.Stream; } if (stream != null) { using (stream) { var gif = GifFile.ReadGifFile(stream, true); stream.Close(); return(gif); } } } else { //using (var wc = new WebClient()) //{ // stream = wc.OpenRead(uri); // if (stream != null) // { // using (stream) // { // var gif = GifFile.ReadGifFile(stream, true); // stream.Close(); // return gif; // } // } // } // TODO: implement using HttpClient } return(null); }
public static BitmapDecoder GetDecoder(string fileName, out GifFile gifFile) { gifFile = null; BitmapDecoder decoder = null; using (var stream = new FileStream(fileName, FileMode.Open)) { stream.Position = 0; decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); if (decoder is GifBitmapDecoder)// && !CanReadNativeMetadata(decoder)) { stream.Position = 0; gifFile = GifFile.ReadGifFile(stream, true); } //if (decoder == null) // throw new InvalidOperationException("Can't get a decoder from the source."); } return(decoder); }
private static async Task <GifFile> DecodeGifFile(Uri uri) { Stream stream = null; if (uri.Scheme == PackUriHelper.UriSchemePack) { StreamResourceInfo sri; if (uri.Authority == "siteoforigin:,,,") { sri = Application.GetRemoteStream(uri); } else { sri = Application.GetResourceStream(uri); } if (sri != null) { stream = sri.Stream; } } else { using (var wc = new HttpClient()) { stream = await wc.GetStreamAsync(uri); } } if (stream != null) { using (stream) { return(GifFile.ReadGifFile(stream, true)); } } return(null); }
private static GifFile DecodeGifFile(Uri uri) { Stream stream = (Stream)null; if (uri.Scheme == PackUriHelper.UriSchemePack) { StreamResourceInfo streamResourceInfo = !(uri.Authority == "siteoforigin:,,,") ? Application.GetResourceStream(uri) : Application.GetRemoteStream(uri); if (streamResourceInfo != null) { stream = streamResourceInfo.Stream; } } else { using (WebClient webClient = new WebClient()) stream = webClient.OpenRead(uri); } if (stream == null) { return((GifFile)null); } using (stream) return(GifFile.ReadGifFile(stream, true)); }
private static BitmapDecoder GetDecoder(BitmapSource image, out GifFile gifFile) { gifFile = (GifFile)null; BitmapDecoder decoder = (BitmapDecoder)null; Stream stream = (Stream)null; Uri result = (Uri)null; BitmapCreateOptions createOptions = BitmapCreateOptions.None; if (image is BitmapImage bitmapImage) { createOptions = bitmapImage.CreateOptions; if (bitmapImage.StreamSource != null) { stream = bitmapImage.StreamSource; } else if (bitmapImage.UriSource != (Uri)null) { result = bitmapImage.UriSource; if (bitmapImage.BaseUri != (Uri)null && !result.IsAbsoluteUri) { result = new Uri(bitmapImage.BaseUri, result); } } } else if (image is BitmapFrame bitmapFrame) { decoder = bitmapFrame.Decoder; Uri.TryCreate(bitmapFrame.BaseUri, bitmapFrame.ToString((IFormatProvider)CultureInfo.InvariantCulture), out result); } if (decoder == null) { if (stream != null) { stream.Position = 0L; decoder = BitmapDecoder.Create(stream, createOptions, BitmapCacheOption.OnLoad); } else if (result != (Uri)null && result.IsAbsoluteUri) { decoder = BitmapDecoder.Create(result, createOptions, BitmapCacheOption.OnLoad); } } if (decoder is GifBitmapDecoder && !ImageBehavior.CanReadNativeMetadata(decoder)) { if (stream != null) { stream.Position = 0L; gifFile = GifFile.ReadGifFile(stream, true); } else { if (!(result != (Uri)null)) { throw new InvalidOperationException("Can't get URI or Stream from the source. AnimatedSource should be either a BitmapImage, or a BitmapFrame constructed from a URI."); } gifFile = ImageBehavior.DecodeGifFile(result); } } if (decoder == null) { throw new InvalidOperationException("Can't get a decoder from the source. AnimatedSource should be either a BitmapImage or a BitmapFrame."); } return(decoder); }
private static BitmapDecoder GetDecoder(BitmapSource image, Image imageControl, out GifFile gifFile) { gifFile = null; BitmapDecoder decoder = null; Stream stream = null; Uri uri = null; BitmapCreateOptions createOptions = BitmapCreateOptions.None; var bmp = image as BitmapImage; if (bmp != null) { createOptions = bmp.CreateOptions; if (bmp.StreamSource != null) { stream = bmp.StreamSource; } else if (bmp.UriSource != null) { uri = bmp.UriSource; if (!uri.IsAbsoluteUri) { var baseUri = bmp.BaseUri ?? (imageControl as IUriContext)?.BaseUri; if (baseUri != null) { uri = new Uri(baseUri, uri); } } } } else { BitmapFrame frame = image as BitmapFrame; if (frame != null) { decoder = frame.Decoder; Uri.TryCreate(frame.BaseUri, frame.ToString(), out uri); } } if (decoder == null) { if (stream != null) { stream.Position = 0; decoder = BitmapDecoder.Create(stream, createOptions, BitmapCacheOption.OnLoad); } else if (uri != null && uri.IsAbsoluteUri) { decoder = BitmapDecoder.Create(uri, createOptions, BitmapCacheOption.OnLoad); } } if (decoder is GifBitmapDecoder && !CanReadNativeMetadata(decoder)) { if (stream != null) { stream.Position = 0; gifFile = GifFile.ReadGifFile(stream, true); } else if (uri != null) { gifFile = DecodeGifFile(uri); } else { throw new InvalidOperationException("Can't get URI or Stream from the source. AnimatedSource should be either a BitmapImage, or a BitmapFrame constructed from a URI."); } } if (decoder == null) { throw new InvalidOperationException("Can't get a decoder from the source. AnimatedSource should be either a BitmapImage or a BitmapFrame."); } return(decoder); }
private static BitmapDecoder GetDecoder(BitmapSource image, out GifFile gifFile) { gifFile = null; BitmapDecoder decoder = null; Stream stream = null; Uri uri = null; BitmapCreateOptions createOptions = BitmapCreateOptions.None; var bmp = image as BitmapImage; if (bmp != null) { createOptions = bmp.CreateOptions; if (bmp.StreamSource != null) { stream = bmp.StreamSource; } else if (bmp.UriSource != null) { uri = bmp.UriSource; if (bmp.BaseUri != null && !uri.IsAbsoluteUri) { uri = new Uri(bmp.BaseUri, uri); } } } else { BitmapFrame frame = image as BitmapFrame; if (frame != null) { decoder = frame.Decoder; Uri.TryCreate(frame.BaseUri, frame.ToString(), out uri); } } if (decoder == null) { if (stream != null) { stream.Position = 0; decoder = BitmapDecoder.Create(stream, createOptions, BitmapCacheOption.OnLoad); } else if (uri != null && uri.IsAbsoluteUri) { decoder = BitmapDecoder.Create(uri, createOptions, BitmapCacheOption.OnLoad); } } if (decoder is GifBitmapDecoder && !CanReadNativeMetadata(decoder)) { if (stream != null) { stream.Position = 0; gifFile = GifFile.ReadGifFile(stream, true); } else if (uri != null) { gifFile = DecodeGifFile(uri); } } return(decoder); }
protected override ImpImage Load(string path, out ImpError error) { error = null; var myUri = new Uri(path, UriKind.RelativeOrAbsolute); var type = GetType(path); BitmapDecoder decoder = null; try { switch (type) { case ImageType.Jpg: try { decoder = new JpegBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } catch (Exception) { var impJpgDecoder = new ImpJpgDecoder(myUri); return(new ImpImage(impJpgDecoder.Source)); } break; case ImageType.Png: try { decoder = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } catch (Exception) { var impPngDecoder = new ImpPngDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); return(new ImpImage(impPngDecoder.Source)); } break; case ImageType.Gif: decoder = new GifBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); if (decoder.Frames.Count > 1) { using (var stream = new FileStream(myUri.AbsolutePath, FileMode.Open)) { var gifFile = GifFile.ReadGifFile(stream, true); return(new ImpImage((GifBitmapDecoder)decoder, gifFile)); } } break; case ImageType.Bmp: decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.Tiff: decoder = new TiffBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.Tga: error = new ImpError(ErrorType.NotSupportedFile); break; case ImageType.Icon: decoder = new IconBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.WindowsMediaPhoto: decoder = new WmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; default: error = new ImpError(ErrorType.UnknownFileType); break; } } catch (Exception) { error = new ImpError(ErrorType.FailedToOpenFile); } if (error != null) { return(null); } return(new ImpImage(decoder)); }