예제 #1
0
        public static void DownloadImage(String url, ResultHandler resultHandler, [CallerMemberName] string caller = null)
        {
            String urll = url.ToLower().Trim();

            try
            {
                if (cachedImage.Contains(urll))
                {
                    resultHandler(cachedImage.Get(urll));
                    return;
                }
                new Task(() =>
                {
                    using (WebClient client = new WebClient())
                    {
                        Console.WriteLine(caller + " downloading sticker...");
                        byte[] data = client.DownloadData(url);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.BeginInit();
                        bitmap.StreamSource = new MemoryStream(data);
                        bitmap.EndInit();

                        if (resultHandler != null)
                        {
                            bitmap.Freeze();
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                cachedImage.AddReplace(urll, bitmap);
                                resultHandler(bitmap);
                            });
                        }
                    }
                }).Start();
            } catch (Exception e)
            {
            }
        }