/// <summary>
        /// 开始分析
        /// </summary>
        public override async Task Action()
        {
            try
            {
                DownloadHelper downloadHelper = new DownloadHelper();
                while (!CancelTokenSource.IsCancellationRequested)
                {
                    TaskSupervisor.DisposeInvalidTask();

                    if (WaitDownLoadImageUrlQueue.Count < 1)
                    {
                        Progress("[图片下载器]图片队列为空...");
                        await Task.Delay(1000);

                        continue;
                    }

                    string imageUrl = Dequeue();
                    if (!ExistImageUrlList.Contains(imageUrl))
                    {
                        Task task   = new Task(() => { Progress("[图片下载器]正在下载图片URL:{0}\t保存路径为:{1}", imageUrl, downloadHelper.DownLoadImage(imageUrl, SaveFolder)); }, CancelTokenSource.Token);
                        bool result = TaskSupervisor.Add(task);
                        if (!result)
                        {
                            Progress("[图片下载器]下载队列已满,等待...", WaitDownLoadImageUrlQueue.Count);
                            await Task.Delay(1000);

                            WaitDownLoadImageUrlQueue.Enqueue(imageUrl);
                            continue;
                        }
                        ExistImageUrlList.Add(imageUrl);
                    }
                }

                TaskSupervisor.DisposeInvalidTask();
            }
            catch (Exception ex)
            {
                Progress("[图片下载器]发生异常:" + ex.ToString());
            }
        }
예제 #2
0
        public override async Task Action()
        {
            CancelTokenSource = new CancellationTokenSource();

            Progress("[图片裁剪器]开始处理...");

            TaskSupervisor.Add(Task.Factory.StartNew(() =>
            {
                try
                {
                    while (!CancelTokenSource.IsCancellationRequested)
                    {
                        TaskSupervisor.DisposeInvalidTask();
                        if (CuttingSettingQueue.Count < 1)
                        {
                            Task.Delay(1000);
                            continue;
                        }

                        CuttingSettingQueue.TryDequeue(out var waitProcessImg);

                        if (!CancelTokenSource.IsCancellationRequested)
                        {
                            if (!TaskSupervisor.Add(new Task(new Action <object>(x =>
                            {
                                if (!CancelTokenSource.IsCancellationRequested)
                                {
                                    var cutterSetting = x as CutterSetting;

                                    if (!CancelTokenSource.IsCancellationRequested)
                                    {
                                        Progress($"[图片裁剪器]正在裁剪图片:{cutterSetting.ImgPath}");

                                        var savePath = cutterSetting.SavePath;
                                        var imgExtensionName = Path.GetExtension(cutterSetting.ImgPath);
                                        if (string.IsNullOrEmpty(savePath))
                                        {
                                            savePath = cutterSetting.ImgPath;
                                        }
                                        if (cutterSetting.ImgPath != savePath)
                                        {
                                            savePath = cutterSetting.ImgPath.Replace(cutterSetting.RootPath, savePath);
                                        }


                                        try
                                        {
                                            #region 裁剪
                                            var cutImg = ImgHelper.CutImg(cutterSetting.ImgPath, cutterSetting.CutWidth, cutterSetting.CutHeight);
                                            #endregion

                                            #region 加水印
                                            if (cutterSetting.IsSetWatermark)
                                            {
                                                switch (cutterSetting.WatermarkType)
                                                {
                                                case WatermarkTypeEnum.Image:
                                                    try
                                                    {
                                                        var watermarkImg = Image.FromFile(cutterSetting.WatermarkTextOrPath);
                                                        if (watermarkImg != null)
                                                        {
                                                            cutImg = Image.FromStream(WaterMarkHelper.AddWatermark(cutImg, imgExtensionName, watermarkImg, cutterSetting.WatermarkPosition));
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Progress($"[图片裁剪器]发生异常:{ex}");
                                                    }

                                                    break;

                                                case WatermarkTypeEnum.Text:
                                                    cutImg = Image.FromStream(WaterMarkHelper.AddWatermark(cutImg, imgExtensionName, cutterSetting.WatermarkTextOrPath, Color.Black, cutterSetting.WatermarkPosition));
                                                    break;

                                                default:
                                                    break;
                                                }
                                            }
                                            #endregion

                                            if (cutImg != null)
                                            {
                                                switch (imgExtensionName)
                                                {
                                                case ".jpg":
                                                    ImageCodecInfo ici = ImgHelper.GetEncoderInfo("image/jpeg");
                                                    if (ici != null)
                                                    {
                                                        EncoderParameters ep = new EncoderParameters(1);
                                                        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);

                                                        cutImg.Save(savePath, ici, ep);
                                                        ep.Dispose();
                                                        ep = null;
                                                        ici = null;
                                                    }
                                                    else
                                                    {
                                                        cutImg.Save(savePath, ImageFormat.Jpeg);
                                                    }
                                                    break;

                                                case ".bmp":
                                                    cutImg.Save(savePath, ImageFormat.Bmp);
                                                    break;

                                                case ".gif":
                                                    cutImg.Save(savePath, ImageFormat.Gif);
                                                    break;

                                                case ".png":
                                                    cutImg.Save(savePath, ImageFormat.Png);
                                                    break;

                                                default:
                                                    cutImg.Save(savePath, ImageFormat.Jpeg);
                                                    break;
                                                }

                                                CutterProgress(TotalCount - CuttingSettingQueue.Count);
                                            }
                                            else
                                            {
                                                Progress($"[图片裁剪器][{cutterSetting.ImgPath}]裁剪时发生未知错误,未能实施裁剪");
                                                CuttingSettingQueue.Enqueue(cutterSetting);
                                            }

                                            cutImg.Dispose();
                                        }
                                        catch (Exception ex)
                                        {
                                            Progress($"[图片裁剪器]发生异常:{ex}");
                                        }
                                    }
                                }
                                else
                                {
                                    Thread.CurrentThread.Abort();
                                }
                            }), waitProcessImg, CancelTokenSource.Token)))
                            {
                                CuttingSettingQueue.Enqueue(waitProcessImg);
                            }
                        }
                        else
                        {
                            Thread.CurrentThread.Abort();
                        }

                        TaskSupervisor.DisposeInvalidTask();
                    }
                }
                catch (Exception ex)
                {
                    Progress($"[图片裁剪器]发生异常:{ex}");
                }
            }, CancelTokenSource.Token));
        }
예제 #3
0
 /// <summary>
 /// 行动!!
 /// </summary>
 /// <param name="url">原始URL</param>
 /// <param name="maxTaskNum">最大任务数量</param>
 public override async Task Action()
 {
     CancelTokenSource = new CancellationTokenSource();
     TaskSupervisor.Add(Task.Factory.StartNew(() => AnalysisBehavior.Action(), CancelTokenSource.Token));
     TaskSupervisor.Add(Task.Factory.StartNew(() => ImgDownloadBehavior.Action(), CancelTokenSource.Token));
 }