/// <summary> /// 异步下载图片 /// </summary> /// <param name="Url"></param> /// <param name="imageType"></param> /// <param name="ID"></param> /// <param name="Cookie"></param> /// <returns></returns> public static async Task <(bool, string)> DownLoadImage(string Url, ImageType imageType, string ID, string Cookie = "", Action <int> callback = null) { if (!Url.IsProperUrl()) { return(false, ""); } bool result = false; string cookies = Cookie; int statuscode = 404; byte[] ImageBytes = null; (ImageBytes, cookies, statuscode) = await Task.Run(() => { (ImageBytes, cookies, statuscode) = DownLoadFile(Url.Replace("\"", "'"), SetCookie: cookies); return(ImageBytes, cookies, statuscode); }); if (ImageBytes == null) { Logger.LogN($"图片下载失败:{Url}"); callback?.Invoke(statuscode); result = false; } else { result = true; ImageProcess.SaveImage(ID, ImageBytes, imageType, Url); } return(result, cookies); }
/// <summary> /// 异步下载图片 /// </summary> /// <param name="Url"></param> /// <param name="imageType"></param> /// <param name="ID"></param> /// <param name="Cookie"></param> /// <returns></returns> public static async Task <(bool, string)> DownLoadImage(string Url, ImageType imageType, string ID, string Cookie = "", Action <int> callback = null) { if (!Url.IsProperUrl()) { return(false, ""); } HttpResult httpResult = await DownLoadFile(Url.Replace("\"", "'"), SetCookie : Cookie); bool result = false; string cookie = ""; if (httpResult == null) { Logger.LogN($" {Jvedio.Language.Resources.DownLoadImageFail}:{Url}"); callback?.Invoke((int)HttpStatusCode.Forbidden); result = false; } else { if (httpResult.Headers.SetCookie != null) { cookie = httpResult.Headers.SetCookie.Split(';')[0]; } else { cookie = Cookie; } result = true; ImageProcess.SaveImage(ID, httpResult.FileByte, imageType, Url); } return(result, cookie); }
private async void DownLoad(object o) { Semaphore.WaitOne(); Actress actress = o as Actress; if (Cancel | actress.id == "") { Semaphore.Release(); return; } try { this.State = DownLoadState.DownLoading; //下载头像 if (!string.IsNullOrEmpty(actress.imageurl)) { string url = actress.imageurl; byte[] imageBytes = null; imageBytes = await Task.Run(() => { return(Net.DownLoadFile(url).filebytes); }); if (imageBytes != null) { ImageProcess.SaveImage(actress.name, imageBytes, ImageType.ActorImage, url); actress.smallimage = ImageProcess.GetBitmapImage(actress.name, "Actresses"); } } //下载信息 bool success = false; success = await Task.Run(() => { Task.Delay(300).Wait(); return(Net.DownActress(actress.id, actress.name, callback: (message) => { MessageCallBack?.Invoke(this, new MessageCallBackEventArgs(message)); })); }); if (success) { actress = DataBase.SelectInfoFromActress(actress); } ProgressBarUpdate.value += 1; InfoUpdate?.Invoke(this, new ActressUpdateEventArgs() { Actress = actress, progressBarUpdate = ProgressBarUpdate, state = State }); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Semaphore.Release(); } }
/// <summary> /// 异步下载图片 /// </summary> /// <param name="Url"></param> /// <param name="imageType"></param> /// <param name="ID"></param> /// <param name="Cookie"></param> /// <returns></returns> public static async Task <(bool, string)> DownLoadImage(string Url, ImageType imageType, string ID, string Cookie = "", Action <int> callback = null) { //如果文件存在则不下载 string filepath = BasePicPath; if (imageType == ImageType.SmallImage) { filepath = Path.Combine(filepath, "SmallPic", ID + ".jpg"); } else if (imageType == ImageType.BigImage) { filepath = Path.Combine(filepath, "BigPic", ID + ".jpg"); } if (File.Exists(filepath)) { return(true, ""); } if (!Url.IsProperUrl()) { return(false, ""); } HttpResult httpResult = await DownLoadFile(Url.Replace("\"", "'"), setCookie : Cookie); bool result = false; string cookie = ""; if (httpResult == null) { Logger.LogN($" {Jvedio.Language.Resources.DownLoadImageFail}:{Url}"); callback?.Invoke((int)HttpStatusCode.Forbidden); result = false; } else { if (httpResult.Headers.SetCookie != null) { cookie = httpResult.Headers.SetCookie.Split(';')[0]; } else { cookie = Cookie; } result = true; ImageProcess.SaveImage(ID, httpResult.FileByte, imageType, Url); } return(result, cookie); }