private void RunDownloadZip(ItemDownload itemDownload)
        {
            //Tạo folder chapter
            string folderChapterPath = DataForm.FolderDownload + @"\" + itemDownload.ComicName + @"\" + itemDownload.ChapterName;

            if (!Directory.Exists(folderChapterPath))
            {
                Directory.CreateDirectory(folderChapterPath);
            }

            //Tải image
            UpdateStatus(itemDownload.ComicName, itemDownload.ChapterName, "Đang tải");
            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            HtmlWeb docHFile = new HtmlWeb();

            html = docHFile.Load(itemDownload.ComicLink);
            WebComic           webComic = DataForm.WebComics.Where(x => itemDownload.ComicLink.Contains(x.web)).FirstOrDefault();
            HtmlNodeCollection nodes    = html.DocumentNode.SelectNodes(webComic.pageChapterXpath);

            if (nodes != null)
            {
                string imageLink, imageType, imagePath;
                for (int i = 0, n = nodes.Count; i < n; i++)
                {
                    imageLink = nodes[i].Attributes["src"].Value;
                    Image imagePageChapter = Common.DownloadImage(imageLink, webComic.headersDownloadImage);
                    if (imagePageChapter != null)
                    {
                        imageType = imageLink.Split('.').Last();
                        imagePath = folderChapterPath + @"\" + Common.ToSafeFileName(nodes[i].Attributes["alt"].Value) + "." + imageType;
                        imagePageChapter.Save(imagePath);
                    }
                }
                ZipMultipleImage(DataForm.FolderDownload + @"\" + itemDownload.ComicName + "\\" + itemDownload.ChapterName + ".zip", folderChapterPath);
            }

            //Save file
            UpdateStatus(itemDownload.ComicName, itemDownload.ChapterName, "Đã xong");
            currentProcessDownloading.Remove(itemDownload);

            //Show notification
            if (!DataForm.OffAlert && !DataDownload.ListDownload.Where(x => x.ComicName.Equals(itemDownload.ComicName) && !x.Status.Equals("Đã xong") && !x.Status.Equals("Lỗi tải ảnh")).Any())
            {
                ShowNotification("Đã tải xong: " + itemDownload.ComicName, folderChapterPath);
            }
        }
        private void RunDownloadPdf(ItemDownload itemDownload)
        {
            //Tạo folder chapter
            string folderChapterPath = DataForm.FolderDownload + @"\" + itemDownload.ComicName;

            if (!Directory.Exists(folderChapterPath))
            {
                Directory.CreateDirectory(folderChapterPath);
            }

            //Tải image
            UpdateStatus(itemDownload.ComicName, itemDownload.ChapterName, "Đang tải");
            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            HtmlWeb docHFile = new HtmlWeb();

            //html = docHFile.Load(itemDownload.ComicLink);
            html = docHFile.LoadFromWebAsync(itemDownload.ComicLink).Result;
            WebComic           webComic = DataForm.WebComics.Where(x => itemDownload.ComicLink.Contains(x.web)).FirstOrDefault();
            HtmlNodeCollection nodes    = html.DocumentNode.SelectNodes(webComic.pageChapterXpath);

            if (nodes != null)
            {
                string      imageLink;
                PdfDocument doc = new PdfDocument();
                if (DataForm.DownloadType == DownloadTypeEnum.MutilChapterOneFilePdf)
                {
                    doc.Info.Title = itemDownload.ChapterName;
                }
                else if (DataForm.DownloadType == DownloadTypeEnum.OneChapterOneFilePdf)
                {
                    doc.Info.Title = itemDownload.ComicName + "-" + itemDownload.ChapterName;
                }

                doc.PageLayout = PdfPageLayout.SinglePage;

                for (int i = 0, n = nodes.Count; i < n; i++)
                {
                    imageLink = nodes[i].Attributes["src"].Value;
                    Image imagePageChapter = Common.DownloadImage(imageLink, webComic.headersDownloadImage);
                    if (imagePageChapter != null)
                    {
                        PdfPage pdfPage = new PdfPage();
                        XSize   size    = PageSizeConverter.ToSize(PageSize.A4);
                        XImage  img     = null;
                        if (imagePageChapter.Width > imagePageChapter.Height)
                        {
                            pdfPage.Orientation = PageOrientation.Landscape;
                            Image _imagePageChapter = ResizeImage(imagePageChapter, (int)size.Height, (int)size.Width - 5, false);
                            img = XImage.FromStream(GetStream(_imagePageChapter, ImageFormat.Jpeg));
                        }
                        else
                        {
                            pdfPage.Orientation = PageOrientation.Portrait;
                            Image _imagePageChapter = ResizeImage(imagePageChapter, (int)size.Width, (int)size.Height - 5, false);
                            img = XImage.FromStream(GetStream(_imagePageChapter, ImageFormat.Jpeg));
                        }

                        pdfPage.Width  = size.Width * 72 / 96;
                        pdfPage.Height = size.Height * 72 / 96;

                        doc.Pages.Add(pdfPage);

                        XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[doc.Pages.Count - 1]);

                        double dx = xgr.PageSize.Width;
                        double dy = xgr.PageSize.Height;
                        xgr.DrawImage(img, (dx - (img.PixelWidth * 72) / 96) / 2, 0);
                    }
                }
                if (doc.Pages.Count == 0)
                {
                    UpdateStatus(itemDownload.ComicName, itemDownload.ChapterName, "Lỗi tải ảnh");
                    currentProcessDownloading.Remove(itemDownload);
                    ShowNotification("Lỗi tải ảnh: " + itemDownload.ComicName + "-" + itemDownload.ChapterName, "", AlertTypeEnum.Error);
                    return;
                }
                doc.Save(folderChapterPath + @"\" + itemDownload.ChapterName + ".pdf");
                doc.Close();
            }

            //Save file
            UpdateStatus(itemDownload.ComicName, itemDownload.ChapterName, "Đã xong");
            currentProcessDownloading.Remove(itemDownload);

            //Merge chapter pdf
            if (!DataDownload.ListDownload.Where(x => x.ComicName.Equals(itemDownload.ComicName) && !x.Status.Equals("Đã xong") && !x.Status.Equals("Lỗi tải ảnh")).Any())
            {
                if (DataForm.DownloadType == DownloadTypeEnum.MutilChapterOneFilePdf)
                {
                    string[] pdfChapters = Directory.GetFiles(folderChapterPath).Where(s => Path.GetExtension(s).ToLowerInvariant() == ".pdf").ToArray();
                    if (pdfChapters == null || pdfChapters.Count() == 0)
                    {
                        ShowNotification("Không tải thành công chapter nào!", folderChapterPath, AlertTypeEnum.Error);
                        return;
                    }
                    MergeMultiplePDFIntoSinglePDF(folderChapterPath + "\\" + itemDownload.ComicName + ".pdf", pdfChapters, itemDownload.ComicName);
                }

                //Show notification
                if (!DataForm.OffAlert)
                {
                    ShowNotification("Đã tải xong: " + itemDownload.ComicName, folderChapterPath);
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            var downloads = DataDownload.ListDownload.Where(x => x.Status == "Chờ tải" && !currentProcessDownloading.Contains(x));

            if (downloads.Any())
            {
                if (!DataForm.IsPause && currentProcessDownloading.Count < DataForm.MaxProcessDownload)
                {
                    //Tải từng chapter
                    ItemDownload itemDownload = downloads.FirstOrDefault();
                    currentProcessDownloading.Add(itemDownload);
                    Thread t = null;
                    switch (DataForm.DownloadType)
                    {
                    case DownloadTypeEnum.MutilChapterOneFilePdf:
                    case DownloadTypeEnum.OneChapterOneFilePdf:
                        t = new Thread(() => RunDownloadPdf(itemDownload));
                        break;

                    case DownloadTypeEnum.OneChapterOneFolderImage:
                        t = new Thread(() => RunDownloadImage(itemDownload));
                        break;

                    case DownloadTypeEnum.OneChapterOneFileZip:
                        t = new Thread(() => RunDownloadZip(itemDownload));
                        break;
                    }
                    t.Start();
                    DataForm.Threads.Add(t);
                    downloading = true;
                }
            }
            else if (downloading)
            {
                bool isAlive = false;
                for (int i = 0, n = DataForm.Threads.Count; i < n; i++)
                {
                    if (DataForm.Threads[i].IsAlive)
                    {
                        isAlive = true;
                        break;
                    }
                }
                if (!isAlive)
                {
                    downloading = false;
                    bool openedAlert = Common.Alert("Đã tải xong toàn bộ", "", AlertTypeEnum.Success);
                    switch (DataForm.Action)
                    {
                    case ActionEnum.Nothing:
                        break;

                    case ActionEnum.Exit:
                        while (openedAlert)
                        {
                            openedAlert     = false;
                            DataForm.Action = ActionEnum.Nothing;
                            Application.Exit();
                        }
                        break;

                    case ActionEnum.Shutdown:
                        SaveSettings();
                        while (openedAlert)
                        {
                            openedAlert     = false;
                            DataForm.Action = ActionEnum.Nothing;
                            Process.Start("shutdown.exe", "-s -t 00");
                        }
                        break;

                    case ActionEnum.Sleep:
                        SaveSettings();
                        while (openedAlert)
                        {
                            openedAlert     = false;
                            DataForm.Action = ActionEnum.Nothing;
                            Process.Start("RUNDLL32.EXE", "powrprof.dll,SetSuspendState 0,1,0");
                        }
                        break;
                    }
                }
            }
        }