コード例 #1
0
        private static async void DownloadImage()
        {
            while (true)
            {
                ImageQueueInfo t = null;
                lock (ImageQueue.Stacks)
                {
                    if (ImageQueue.Stacks.Count > 0)
                    {
                        t = ImageQueue.Stacks.Dequeue();
                    }
                }

                if (t != null)
                {
                    Uri         uri   = new Uri(t.url);
                    BitmapImage image = null;
                    try
                    {
                        if ("http".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase))
                        {
                            //如果是HTTP下载文件
                            var    service = new PhotoImageService();
                            Byte[] imgData = await service.Download(t.url);

                            if (null != imgData)
                            {
                                using (var ms = new MemoryStream(imgData))
                                {
                                    image = new BitmapImage();
                                    image.BeginInit();
                                    image.CacheOption  = BitmapCacheOption.OnLoad;
                                    image.StreamSource = ms;
                                    image.EndInit();
                                }
                            }
                        }
                        else if ("file".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase))
                        {
                            using (var fs = new FileStream(t.url, FileMode.Open))
                            {
                                image = new BitmapImage();
                                image.BeginInit();
                                image.CacheOption  = BitmapCacheOption.OnLoad;
                                image.StreamSource = fs;
                                image.EndInit();
                            }
                        }

                        if (image != null)
                        {
                            if (image.CanFreeze)
                            {
                                image.Freeze();
                            }

                            await t.image.Dispatcher.BeginInvoke(new Action <ImageQueueInfo, BitmapImage>((i, bmp) =>
                            {
                                if (ImageQueue.OnComplete != null)
                                {
                                    ImageQueue.OnComplete(i.image, i.url, bmp);
                                }
                            }), new Object[] { t, image });
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.MessageBox.Show(e.Message);
                        continue;
                    }
                }

                if (ImageQueue.Stacks.Count > 0)
                {
                    continue;
                }
                autoEvent.WaitOne();
            }
        }
コード例 #2
0
 private static void OnSourceWithSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
 {
     ImageQueue.Queue((Image)o, (string)e.NewValue);
 }