private void OnWindowLoaded(object sender, RoutedEventArgs e) { QueryDepartment(); QueryRules(); QueryCameras(); if (current != null) { if (current.FirstPhoto != null) { ImageDecoder.SetSource(imageFirst, PhotoImageService.GetImageFileURL(current.FirstPhoto.ID)); } if (current.SecondPhoto != null) { ImageDecoder.SetSource(imageSecond, PhotoImageService.GetImageFileURL(current.SecondPhoto.ID)); } if (current.ThirdPhoto != null) { ImageDecoder.SetSource(imageThird, PhotoImageService.GetImageFileURL(current.ThirdPhoto.ID)); } } }
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(); } }