Exemplo n.º 1
0
 private void DownloadImage()
 {
     while (true)
     {
         ImageQueueInfo t = null;
         lock (this.Stacks)
         {
             if (this.Stacks.Count > 0)
             {
                 t = this.Stacks.Dequeue();
             }
         }
         if (t != null)
         {
             if (!File.Exists(t.FullName))
             {
                 WebHelper.DownImage(t.Url, t.FullName);
             }
         }
         if (this.Stacks.Count > 0)
         {
             continue;
         }
         autoEvent.WaitOne();
     }
 }
Exemplo n.º 2
0
 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);
             ImageSource image = null;
             try
             {
                 if ("file".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase))
                 {
                     image = Util.GetBitmapImageForBackUp(t.url);
                 }
                 if (image != null)
                 {
                     if (image.CanFreeze)
                     {
                         image.Freeze();
                     }
                     t.image.Dispatcher.BeginInvoke(new Action <ImageQueueInfo, ImageSource>((i, bmp) =>
                     {
                         if (ImageQueue.OnComplate != null)
                         {
                             ImageQueue.OnComplate(i.image, i.url, image);
                         }
                     }), new Object[] { t, image });
                 }
             }
             catch (Exception e)
             {
                 continue;
             }
         }
         if (ImageQueue.Stacks.Count > 0)
         {
             continue;
         }
         autoEvent.WaitOne();
     }
 }
Exemplo n.º 3
0
 /// <summary>
 ///
 /// </summary>
 private static void DownloadImage()
 {
     while (true)
     {
         ImageQueueInfo t = null;
         lock (Stacks)
         {
             if (Stacks.Count > 0)
             {
                 t = Stacks.Dequeue();
             }
         }
         if (t != null)
         {
             Uri         uri   = new Uri(t.url);
             BitmapImage image = null;
             try
             {
                 if ("http".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase))
                 {
                     //如果是HTTP下载文件
                     using (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))
                 {
                     if (!File.Exists(t.url))
                     {
                         continue;
                     }
                     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) =>
                     {
                         OnComplate?.Invoke(i.image, i.url, bmp);
                     }), new object[] { t, image });
                 }
             }
             catch (Exception e)
             {
                 MessageBox.Show(e.Message);
                 continue;
             }
         }
         if (Stacks.Count > 0)
         {
             continue;
         }
         autoEvent.WaitOne();
     }
 }
Exemplo n.º 4
0
        private static async 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下载文件
                            var    service = new PhotoImageService();
                            Byte[] imgData = await service.Download(t.url);

                            if (null != imgData)
                            {
                                using (var ms = new MemoryStream(imgData))
                                {
                                    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();
                            }

                            await t.image.Dispatcher.BeginInvoke(new Action <ImageQueueInfo, BitmapImage>((i, bmp) =>
                            {
                                if (ImageQueue.OnComplete != null)
                                {
                                    ImageQueue.OnComplete(i.image, i.url, bmp);
                                }
                            }), new Object[] { t, image });
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.MessageBox.Show(e.Message);
                        continue;
                    }
                }

                if (ImageQueue.Stacks.Count > 0)
                {
                    continue;
                }
                autoEvent.WaitOne();
            }
        }
Exemplo n.º 5
0
        private void Monitor()
        {
            ThreadPool.SetMinThreads(1, 1);
            ThreadPool.SetMaxThreads(4, 4);
            while (true)
            {
                ImageQueueInfo t = null;
                lock (this.Stacks)
                {
                    if (this.Stacks.Count > 0)
                    {
                        t = this.Stacks.Dequeue();
                    }
                }
                if (t != null)
                {
                    switch (t.State)
                    {
                    case TaskState.None:
                        ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadImage), t);
                        break;

                    case TaskState.Complate:
                        if (this.OnComplate != null && t.BitmapImage != null)
                        {
                            System.Windows.FrameworkElement ele = t.Image;
                            if (ele == null)
                            {
                                ele = this.pElement;
                            }

                            if (ele != null)
                            {
                                ele.Dispatcher.BeginInvoke(this.OnComplate, new Object[] { t.Image, t.BitmapImage, t.ImgInfo });
                            }
                        }
                        break;

                    case TaskState.Error:
                        if (this.OnError != null)
                        {
                            System.Windows.FrameworkElement ele = t.Image;
                            if (ele == null)
                            {
                                ele = this.pElement;
                            }

                            if (ele != null)
                            {
                                ele.Dispatcher.BeginInvoke(this.OnError, new Object[] { t.Image, t.Error });
                            }
                        }
                        break;
                    }
                }
                if (this.Stacks.Count > 0)
                {
                    continue;
                }
                autoEvent.WaitOne();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 下载方法
        /// </summary>
        private void DownloadImage()
        {
            while (true)
            {
                ImageQueueInfo t = null;
                lock (this.Stacks)
                {
                    if (this.Stacks.Count > 0)
                    {
                        t = this.Stacks.Dequeue();
                    }
                }
                if (t != null)
                {
                    Uri         uri    = new Uri(t.Url);
                    BitmapImage bImage = null;
                    try
                    {
                        bool   local        = false;
                        string fileFullName = string.Empty;
                        if (!string.IsNullOrEmpty(this.CachePath))
                        {
                            fileFullName = Path.Combine(this.CachePath, t.Name);
                            local        = File.Exists(fileFullName);
                        }

                        if (!local && "http".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase))
                        {
                            //如果是HTTP下载文件
                            WebClient wc = new WebClient();
                            using (var ms = new MemoryStream(wc.DownloadData(uri)))
                            {
                                // 是否设置了缓存路径,有就先保存到缓存目录
                                if (!string.IsNullOrEmpty(this.CachePath))
                                {
                                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                                    if (image != null)
                                    {
                                        if (!File.Exists(fileFullName))
                                        {
                                            image.Save(fileFullName);
                                        }
                                        image.Dispose();
                                        local = true;
                                    }
                                }
                                // 是否有完成后的委托,有就获取BitmapImage
                                else if (this.OnComplate != null)
                                {
                                    bImage = new BitmapImage();
                                    bImage.BeginInit();
                                    bImage.CacheOption  = BitmapCacheOption.OnLoad;
                                    bImage.StreamSource = ms;
                                    bImage.EndInit();
                                }
                            }
                        }
                        // 从本地获取
                        if (local && this.OnComplate != null)
                        {
                            while (WinApi.FileIsOccupy(fileFullName))
                            {
                                Thread.Sleep(500);
                            }
                            using (var fs = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                bImage = new BitmapImage();
                                bImage.BeginInit();
                                bImage.CacheOption  = BitmapCacheOption.OnLoad;
                                bImage.StreamSource = fs;
                                bImage.EndInit();
                            }
                        }
                        if (bImage != null && this.OnComplate != null && t.image != null)
                        {
                            if (bImage.CanFreeze)
                            {
                                bImage.Freeze();
                            }
                            t.image.Dispatcher.BeginInvoke(new Action <Image, BitmapImage, string, ImgInfo>((image, bmp, name, imgInfo) =>
                            {
                                image.Tag = name;
                                this.OnComplate(bmp, imgInfo);
                            }), new Object[] { t.image, bImage, t.Name, t.ImgInfo });
                        }
                    }
                    catch (Exception e)
                    {
                        LogHelper.WriteLog(e.Message, EnumLogLevel.Error);
                        if (this.OnError != null)
                        {
                            t.image.Dispatcher.BeginInvoke(new Action <Exception>((ex) =>
                            {
                                this.OnError(ex);
                            }), new Object[] { e });
                        }
                    }
                }
                if (this.Stacks.Count > 0)
                {
                    continue;
                }
                autoEvent.WaitOne();
            }
        }
Exemplo n.º 7
0
        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();
            }
        }