Exemple #1
0
        private static void DownloadImageAsync(ImageSequence imageSequenceToLoadInto, string uriToLoad, Action doneLoading, ImageSequence asyncImageSequence, string pngFileName, string gifFileName)
        {
            WebClient client = new WebClient();

            client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) =>
            {
                try                 // if we get a bad result we can get a target invocation exception. In that case just don't show anything
                {
                    Task.Run(() =>
                    {
                        // scale the loaded image to the size of the target image
                        byte[] raw    = e.Result;
                        Stream stream = new MemoryStream(raw);

                        lock (locker)
                        {
                            StaticData.Instance.LoadImageSequenceData(stream, asyncImageSequence);
                        }

                        if (asyncImageSequence.Frames.Count == 1)
                        {
                            // save the as png
                            lock (locker)
                            {
                                if (!File.Exists(pngFileName))
                                {
                                    ImageIO.SaveImageData(pngFileName, asyncImageSequence.Frames[0]);
                                }
                            }
                        }
                        else                         // save original stream as gif
                        {
                            using (var writter = new FileStream(gifFileName, FileMode.Create))
                            {
                                stream.Position = 0;
                                stream.CopyTo(writter);
                            }
                        }

                        UiThread.RunOnIdle(() =>
                        {
                            imageSequenceToLoadInto.Copy(asyncImageSequence);
                            imageSequenceToLoadInto.Invalidate();
                            doneLoading?.Invoke();
                        });
                    });
                }
                catch
                {
                }
            };

            try
            {
                client.DownloadDataAsync(new Uri(uriToLoad));
            }
            catch
            {
            }
        }
Exemple #2
0
        /// <summary>
        /// Download an image from the web into the specified ImageSequence
        /// </summary>
        /// <param name="uri"></param>
        public static void RetrieveImageSquenceAsync(ImageSequence imageSequenceToLoadInto,
                                                     string uriToLoad,
                                                     Action doneLoading = null)
        {
            var asyncImageSequence = new ImageSequence();

            var longHash    = uriToLoad.GetLongHashCode();
            var pngFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png");
            var gifFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".gif");

            if (File.Exists(pngFileName))
            {
                try
                {
                    Task.Run(() =>
                    {
                        AggContext.StaticData.LoadImageSequenceData(new StreamReader(pngFileName).BaseStream, asyncImageSequence);
                        UiThread.RunOnIdle(() =>
                        {
                            imageSequenceToLoadInto.Copy(asyncImageSequence);
                            imageSequenceToLoadInto.Invalidate();
                            doneLoading?.Invoke();
                        });
                    });

                    return;
                }
                catch
                {
                }
            }
            else if (File.Exists(gifFileName))
            {
                Task.Run(() =>
                {
                    try
                    {
                        AggContext.StaticData.LoadImageSequenceData(new StreamReader(gifFileName).BaseStream, asyncImageSequence);
                        if (asyncImageSequence.NumFrames > 0)
                        {
                            UiThread.RunOnIdle(() =>
                            {
                                imageSequenceToLoadInto.Copy(asyncImageSequence);
                                imageSequenceToLoadInto.Invalidate();
                                doneLoading?.Invoke();
                            });
                        }
                        else
                        {
                            DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
                        }
                    }
                    catch
                    {
                        DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
                    }
                });

                return;
            }

            DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
        }
Exemple #3
0
        /// <summary>
        /// Download an image from the web into the specified ImageSequence
        /// </summary>
        /// <param name="uri"></param>
        public static void RetrieveImageSquenceAsync(ImageSequence imageSequenceToLoadInto, string uriToLoad)
        {
            var asyncImageSequence = new ImageSequence();

            var longHash    = uriToLoad.GetLongHashCode();
            var pngFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png");
            var gifFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".gif");

            if (File.Exists(pngFileName))
            {
                try
                {
                    Task.Run(() =>
                    {
                        AggContext.StaticData.LoadImageSequenceData(new StreamReader(pngFileName).BaseStream, asyncImageSequence);
                        UiThread.RunOnIdle(() =>
                        {
                            imageSequenceToLoadInto.Copy(asyncImageSequence);
                            imageSequenceToLoadInto.Invalidate();
                        });
                    });

                    return;
                }
                catch
                {
                }
            }
            else if (File.Exists(gifFileName))
            {
                try
                {
                    Task.Run(() =>
                    {
                        AggContext.StaticData.LoadImageSequenceData(new StreamReader(gifFileName).BaseStream, asyncImageSequence);
                        UiThread.RunOnIdle(() =>
                        {
                            imageSequenceToLoadInto.Copy(asyncImageSequence);
                            imageSequenceToLoadInto.Invalidate();
                        });
                    });

                    return;
                }
                catch
                {
                }
            }

            WebClient client = new WebClient();

            client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) =>
            {
                try                 // if we get a bad result we can get a target invocation exception. In that case just don't show anything
                {
                    Task.Run(() =>
                    {
                        // scale the loaded image to the size of the target image
                        byte[] raw    = e.Result;
                        Stream stream = new MemoryStream(raw);

                        AggContext.StaticData.LoadImageSequenceData(stream, asyncImageSequence);

                        if (asyncImageSequence.Frames.Count == 1)
                        {
                            // save the as png
                            AggContext.ImageIO.SaveImageData(pngFileName, asyncImageSequence.Frames[0]);
                        }
                        else                         // save original stream as gif
                        {
                            using (var writter = new FileStream(gifFileName, FileMode.Create))
                            {
                                stream.Position = 0;
                                stream.CopyTo(writter);
                            }
                        }

                        UiThread.RunOnIdle(() =>
                        {
                            imageSequenceToLoadInto.Copy(asyncImageSequence);
                            imageSequenceToLoadInto.Invalidate();
                        });
                    });
                }
                catch
                {
                }
            };

            try
            {
                client.DownloadDataAsync(new Uri(uriToLoad));
            }
            catch
            {
            }
        }