private static void OnSourceWithSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { string allpath = (string)e.NewValue; string[] result = allpath.Split('|'); string url = result[0]; string resType = result.Length > 1 ? result[1] : "0"; bool localimage = result.Length > 2 ? (allpath.Split('|')[2] == "1") : true; string title = result.Length > 3 ? result[3] : "找不到名字"; string resKey = result.Length > 4 ? result[4] : "0"; ImageQueue.Queue((Image)o, url, localimage, title, resKey, resType); }
private static void DownloadImage() { while (true) { ImageQueueInfo t = null; lock (ImageQueue.Stacks) { if (ImageQueue.Stacks.Count > 0) { t = ImageQueue.Stacks.Dequeue(); } } if (t != null) { try { BitmapImage image = null; if (t.localImage) { //image = new BitmapImage(new Uri("Resources/头像2.png", UriKind.Relative)); string path = ""; if (t.resType == ResType.Doc.GetDBCode()) { path = @"\Resources\video_img.png"; } else if (t.resType == ResType.Vedio.GetDBCode()) { path = @"\Resources\book_img.png"; } else if (t.resType == ResType.Web.GetDBCode()) { path = @"\Resources\web_img.png"; } using (var fs = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + path, FileMode.Open)) { image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = fs; image.EndInit(); } } else { Uri uri = new Uri(t.url); if ("http".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase)) { //如果是HTTP下载文件 WebClient wc = new WebClient(); using (var ms = new MemoryStream(wc.DownloadData(uri))) { 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(); } t.image.Dispatcher.BeginInvoke(new Action <ImageQueueInfo, BitmapImage>((i, bmp) => { if (ImageQueue.OnComplate != null) { ImageQueue.OnComplate(i.image, i.url, bmp, i.title, i.resKey); } }), new Object[] { t, image }); } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message); continue; } } if (ImageQueue.Stacks.Count > 0) { continue; } autoEvent.WaitOne(); } }