public void GetAverageColorByLocalFile() { singleClassForPage o = SelectedPreview; string localName = ""; if (GlobalImageList.instance.ImageList.Count > 0 && o != null) { localName = GlobalImageList.instance.ImageList.Where(j => j.Item1 == ImageSave.GetFileName(o.Image_url)).LastOrDefault().Item2; } try { BitmapImage bit = new BitmapImage(new Uri(localName, UriKind.RelativeOrAbsolute)); SolidColorBrush s = GetAverageColor(bit); SelectedPreview.BackGround = s; } catch { } }
private static 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下载文件 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) => { ImageQueue.OnComplate?.Invoke(i.image, i.url, bmp); }), new Object[] { t, image }); ImageSave.Save(t.url, image); } } catch (Exception e) { continue; } } if (ImageQueue.Stacks.Count > 0) { continue; } autoEvent.WaitOne(); } }