Exemplo n.º 1
0
        /// <summary>
        /// 下载图片
        /// </summary>
        public void DownloadImg()
        {
            if (PreFetcher.Fetcher.PreFetchedImg(img.SampleUrl) != null)
            {
                preview.Source = PreFetcher.Fetcher.PreFetchedImg(img.SampleUrl);
                //preview.Source = BitmapDecoder.Create(PreFetcher.Fetcher.PreFetchedImg(img.PreUrl), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
            }
            else
            {
                try
                {
                    req       = Sweb.CreateWebRequest(img.SampleUrl, MainWindow.WebProxy, shc);
                    req.Proxy = MainWindow.WebProxy;

                    //异步下载开始
                    req.BeginGetResponse(new AsyncCallback(RespCallback), req);
                }
                catch (Exception ex)
                {
                    Program.Log(ex, "Start download preview failed");
                    preview_ImageFailed(null, null);
                }
            }

            if (!isDetailSucc && img.DownloadDetail != null)
            {
                canRetry           = true;
                isRetrievingDetail = true;
                chk.Text           = "信息加载中...";
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((o) =>
                {
                    try
                    {
                        img.DownloadDetail(img, MainWindow.WebProxy);
                        Dispatcher.Invoke(new VoidDel(() =>
                        {
                            LayoutRoot.IsEnabled = true;

                            ShowImgDetail();

                            isRetrievingDetail = false;
                            if (imgLoaded && ImgLoaded != null)
                            {
                                ImgLoaded(index, null);
                            }
                        }));
                    }
                    catch (Exception ex)
                    {
                        Program.Log(ex, "Download img detail failed");
                        Dispatcher.Invoke(new VoidDel(() =>
                        {
                            preview_ImageFailed(null, null);
                            isRetrievingDetail = false;
                            canRetry           = true;
                            chk.Text           = "信息加载失败";
                            if (imgLoaded && ImgLoaded != null)
                            {
                                ImgLoaded(index, null);
                            }
                        }));
                    }
                }));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 下载图片
        /// </summary>
        public void DownloadImg()
        {
            if (PreFetcher.Fetcher.PreFetchedImg(img.PreviewUrl) != null)
            {
                preview.Source = PreFetcher.Fetcher.PreFetchedImg(img.PreviewUrl);
                //preview.Source = BitmapDecoder.Create(PreFetcher.Fetcher.PreFetchedImg(img.PreUrl), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
            }
            else
            {
                try
                {
                    req       = (HttpWebRequest)WebRequest.Create(img.PreviewUrl);
                    req.Proxy = MainWindow.WebProxy;

                    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
                    if (!string.IsNullOrWhiteSpace(needReferer))
                    {
                        //req.Referer = img.PreUrl.Substring(0, img.PreUrl.IndexOf('/', 7) + 1);
                        req.Referer = needReferer;
                    }

                    req.CookieContainer = Sweb.CookieContainer;

                    //异步下载开始
                    req.BeginGetResponse(new AsyncCallback(RespCallback), req);
                }
                catch (Exception ex)
                {
                    Program.Log(ex, "Start download preview failed");
                    StopLoadImg();
                }
            }

            if (!isDetailSucc && img.DownloadDetail != null)
            {
                isRetrievingDetail = true;
                chk.Text           = "信息加载中...";
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((o) =>
                {
                    try
                    {
                        img.DownloadDetail(img, MainWindow.WebProxy);
                        Dispatcher.Invoke(new VoidDel(() =>
                        {
                            LayoutRoot.IsEnabled = true;

                            ShowImgDetail();

                            isRetrievingDetail = false;
                            if (imgLoaded && ImgLoaded != null)
                            {
                                ImgLoaded(index, null);
                            }
                        }));
                    }
                    catch (Exception ex)
                    {
                        Program.Log(ex, "Download img detail failed");
                        Dispatcher.Invoke(new VoidDel(() =>
                        {
                            isRetrievingDetail = false;
                            canRetry           = true;
                            chk.Text           = "信息加载失败";
                            if (imgLoaded && ImgLoaded != null)
                            {
                                ImgLoaded(index, null);
                            }
                        }));
                    }
                }));
            }
        }