private void ImportTile_Click(object sender, RoutedEventArgs e)
 {
     this.Move = (bool)cbMv.IsChecked;
     var task = Task.Factory.StartNew(() =>
                {
                    if (!System.IO.Directory.Exists(Global.CollectionDir + "Imports"))
                        System.IO.Directory.CreateDirectory(Global.CollectionDir + "Imports");
                    Dispatcher.Invoke(new EmptyDelegate(() =>
                    {
                        this.pbar.Value = 0;
                    }));
                    int i = 0;
                    int tot = ImportList.Items.Count;
                    while (ImportList.Items.Count > 0)
                    {
                        string name = string.Empty;
                        string path = ImportList.Items[i].ToString();
                        name = (new FileInfo(System.IO.Path.GetDirectoryName(path))).Directory.Name;
                        if (!System.IO.Directory.Exists(Global.CollectionDir + "Imports\\" + name))
                        {
                            System.IO.Directory.CreateDirectory(Global.CollectionDir + "Imports\\" + name);
                            Manga manga = new Manga();
                            manga.Author = "Unknown";
                            manga.Description = "Unknown";
                            manga.MangaName = name;
                            if (!Move)
                            {
                                Book b = new Book(manga);
                                b.Name = name;
                                b.Filename = name + ".bin";
                                foreach (var file in System.IO.Directory.EnumerateFiles(path))
                                {
                                    File.Copy(file, Global.CollectionDir + "Imports\\" + name + "\\" + System.IO.Path.GetFileName(file));
                                    b.PageLocalUrls.Add(Global.CollectionDir + "Imports\\" + name + "\\" + System.IO.Path.GetFileName(file));
                                }
                                using (var fs = new FileStream(Global.CollectionDir + "Imports\\" + name + "\\" + b.Filename, FileMode.Open))
                                {
                                    BinaryFormatter bf = new BinaryFormatter();
                                    bf.Serialize(fs, b);
                                    fs.Close();
                                }
                            }
                        }
                        Dispatcher.Invoke(new UpdateDelegate((Cur, Tot) =>
                        {
                            int pre = ((Cur * 100) / Tot);
                            this.pbar.Value = pre;
                        }), i, tot);
                    }
                    Dispatcher.Invoke(new EmptyDelegate(() =>
                    {
                        this.pbar.Value = 0;
                    }));
                });
 }
        public Book GetBook(Manga mag, BookEntry chapter, Action<int,int> progressHandler = null)
        {
            Book b = new Book(mag);
            b.Name = chapter.Name;
            b.ReleaseDate = chapter.ReleaseDate;
            b.Pages = new System.Collections.ObjectModel.Collection<object>();
            b.PageOnlineUrls = new System.Collections.ObjectModel.Collection<Uri>();

            string url = chapter.Url; //first page.
            string firstpagehtml = GetHtml(url);

            int maxpages = 0;
            Match pgcount = Regex.Match(firstpagehtml, "<div id=\"selectpage\">.+?</div>", RegexOptions.Singleline | RegexOptions.Compiled);
            string pgcountstr = Regex.Replace(pgcount.Value, "<option.+?>.+?</option>", "");
            pgcountstr = Regex.Replace(pgcountstr, "<.+?>", "");
            pgcountstr = pgcountstr.Replace("of", "").Replace(" ", "").Replace("\n", "");
            maxpages = int.Parse(pgcountstr);

            Match firstimg = Regex.Match(firstpagehtml, "<img id=\"img\".+?>", RegexOptions.Singleline | RegexOptions.Compiled);
            string firstimgurl = Regex.Match(firstimg.Value, "src=\".+?\"", RegexOptions.Singleline | RegexOptions.Compiled).Value;
            firstimgurl = Regex.Replace(firstimgurl, "(src=\"|\")", "");
            System.Windows.Application.Current.Dispatcher.Invoke(new EmptyDelegate(delegate()
            {
                var uri = new Uri(firstimgurl);
                b.PageOnlineUrls.Add(uri);
                /*
                using (var ms = new MemoryStream())
                {

                    if (uri.LocalPath.EndsWith(".png"))
                    {
                        PngBitmapEncoder png = new PngBitmapEncoder();
                        png.Frames.Add(BitmapFrame.Create(uri));
                        png.Save(ms);
                    }
                    else if (uri.LocalPath.EndsWith(".jpg"))
                    {
                        JpegBitmapEncoder jpg = new JpegBitmapEncoder();
                        jpg.Frames.Add(BitmapFrame.Create(uri));
                        jpg.Save(ms);
                    }
                    else if (uri.LocalPath.EndsWith(".bmp"))
                    {
                        BmpBitmapEncoder bmp = new BmpBitmapEncoder();
                        bmp.Frames.Add(BitmapFrame.Create(uri));
                        bmp.Save(ms);
                    }
                    ms.Flush();
                    b.Pages.Add(ms.ToArray());
                } */
            }), null);

            string url_1 = null;
            string url_2 = null;
            if (url.EndsWith(".html"))
            {
                //old style urls.
                url_1 = url.Substring(0, url.NthIndexOf("-", 2) + 1);
                url_2 = url.Substring(url.NthIndexOf("/", 4));
            }
            else
            {
                url_1 = url + "/";
                url_2 = "";
            }
            for (int i = 2; i < maxpages + 1; i++)
            {
                if (progressHandler != null)
                    progressHandler(i, maxpages);

                string purl = url_1 + i + url_2;
                string html = GetHtml(purl);

                Match img = Regex.Match(html, "<img id=\"img\".+?>", RegexOptions.Singleline |  RegexOptions.Compiled);
                string imgurl = Regex.Match(img.Value, "src=\".+?\"", RegexOptions.Singleline | RegexOptions.Compiled).Value;
                imgurl = Regex.Replace(imgurl, "(src=\"|\")", "");
                System.Windows.Application.Current.Dispatcher.Invoke(new EmptyDelegate(delegate()
                {
                    var uri = new Uri(imgurl);
                    b.PageOnlineUrls.Add(uri);
                    /*
                    using (var ms = new MemoryStream())
                    {

                        if (uri.LocalPath.EndsWith(".png"))
                        {
                            PngBitmapEncoder png = new PngBitmapEncoder();
                            png.Frames.Add(BitmapFrame.Create(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default));
                            png.Save(ms);
                        }
                        else if (uri.LocalPath.EndsWith(".jpg"))
                        {
                            JpegBitmapEncoder jpg = new JpegBitmapEncoder();
                            jpg.Frames.Add(BitmapFrame.Create(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default));
                            jpg.Save(ms);
                        }
                        else if (uri.LocalPath.EndsWith(".bmp"))
                        {
                            BmpBitmapEncoder bmp = new BmpBitmapEncoder();
                            bmp.Frames.Add(BitmapFrame.Create(uri, BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default));
                            bmp.Save(ms);
                        }
                        ms.Flush();
                        b.Pages.Add(ms.ToArray());
                    }
                     * */
                }), null);

                System.Threading.Thread.Sleep(50); //Decreased from 100 to 50, better download time,   // //Prevent simulating a DDOS.
            }

            return b;
        }