async Task CheckPath(CacheImage image, string url) { if (url == null) { return; } if (Uri.TryCreate(url, UriKind.Absolute, out Uri res)) { SetImage(image, url); } else { try { var download = await _core.HttpClient.DownloadDataAsync(url); if (download != null) { await SetImagePath(image, download); } } catch { //set default } } }
private static void FinaliseTexture(string url, ulong codedURL, Texture2D tex, Action <ServerImage> successCallback, string cancelKey) { Debugger.Log("FinaliseTexture key: " + cancelKey + " url: " + url, (int)SharedSystems.Systems.WEB_IMAGE); CacheImage newCacheImage = new CacheImage(); WeakReference weakReference = new WeakReference(tex); newCacheImage.weakReference = weakReference; newCacheImage.isCached = false; newCacheImage.timeStamp = System.DateTime.UtcNow; if (RemoveRequest(cancelKey, codedURL)) { try { ServerImage image = new ServerImage(); image.texture = newCacheImage.weakReference.Target as Texture2D; image.isCached = false; Debugger.Log("FinaliseTexture successCallback key: " + cancelKey + " url: " + url, (int)SharedSystems.Systems.WEB_IMAGE); successCallback(image); } catch (Exception ex) { Debugger.Error("FinaliseTexture Error in success callback (" + cancelKey + "): " + ex.Message, (int)SharedSystems.Systems.WEB_IMAGE); } } else { Debugger.Log("FinaliseTexture RemoveRequestFailed url: " + url, (int)SharedSystems.Systems.WEB_IMAGE); } AddImageToCache(newCacheImage, codedURL); }
/// <summary> /// 需要验证码,打码 /// </summary> private void verfiyCode() { CacheImage cacheImage = new CacheImage(); CacheImage.MyDelegate myDelegate = new CacheImage.MyDelegate(imgcheckCode); cacheImage.GetCacheImage(webBrower, "imgcheckcode", myDelegate, this); }
void Start() { string url = "http://www.shijunzh.com/wp-content/uploads/2017/06/cropped-icon.png"; string name = CacheImage.GetMD5WithString(url); CacheImage.Cache(this, CacheEvent).DownLoad(url, name); }
private static void AddImageToCache(CacheImage image, ulong codedURL) { if (!s_cachedImages.ContainsKey(codedURL)) { s_cachedImages.Add(codedURL, image); } else if (s_cachedImages[codedURL].weakReference.Target == null) { s_cachedImages[codedURL] = image; } }
async Task SetImage(CacheImage image, string url) { var uri = new Uri(new Uri("https://cdn.pixabay.com"), url); var download = await new WebClient().DownloadDataTaskAsync(uri); var img2 = new BitmapImage(); img2.BeginInit(); img2.CreateOptions |= BitmapCreateOptions.IgnoreColorProfile; img2.CacheOption = BitmapCacheOption.OnLoad; img2.StreamSource = new MemoryStream(download); img2.EndInit(); img2.Freeze(); image.Source = img2; }
private void ImageFromDemoPage_Loaded(object sender, System.Windows.RoutedEventArgs e) { CacheImage img = new CacheImage() { ImageUrl = (DataContext as ImageViewModel).Url, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch }; Grid g = new Grid() { Background = Brushes.White }; g.Children.Add(img); Content = g; }
void SetImage(CacheImage image, string url) { try { var img2 = new BitmapImage(); img2.BeginInit(); img2.CreateOptions |= BitmapCreateOptions.IgnoreColorProfile; img2.CacheOption = BitmapCacheOption.OnLoad; img2.UriSource = new Uri(url); img2.EndInit(); //img2.Freeze(); img2.DownloadCompleted += Img2_DownloadCompleted; image.Source = img2; } catch (Exception e) { _core.Logger.Error(this, e.Message); //SetDefaultImage(); } }
private async Task SetImagePath(CacheImage image, byte[] bytes) { try { BitmapImage img2 = null; await Task.Run(() => { img2 = new BitmapImage(); img2.BeginInit(); img2.CreateOptions |= BitmapCreateOptions.IgnoreColorProfile; img2.CacheOption = BitmapCacheOption.OnLoad; img2.StreamSource = new MemoryStream(bytes); img2.EndInit(); img2.Freeze(); }); image.Source = img2; } catch (Exception e) { _core.Logger.Error(this, e.Message); //SetDefaultImage(); } }