Exemplo n.º 1
0
        private ArticleImage CreateFakeArticleImage(Chapter chapter)
        {
            var image = new ArticleImage();
          //  image.Chapter = chapter;

            var bitMapImage = new BitmapImage();

            GifDecoder gd = new GifDecoder();
            var img = new ImageTools.ExtendedImage();

            gd.Decode(img, Application.GetResourceStream(
                new Uri("/SmartReader.Library;component/Resource/8004887.gif", UriKind.Relative)
                ).Stream);

            var png = new PngEncoder();
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            using (MemoryStream stream = new MemoryStream())
            {
                png.Encode(img, stream);
                bitmap.SetSource(stream);
            }

            image.ImageBytes = ConvertToBytes(bitmap);
            Storage.SaveArticleImages(new[] { image });

            return image;
        }
        private void GetImageContents(List<HtmlNode> imageNodes)
        {
            List<string> urlList = new List<string>();

            foreach (var imageNode in imageNodes)
            {
                if (imageNode.Attributes["src"] != null)
                {
                    var url = imageNode.Attributes["src"].Value;

                    if (!url.Contains("http:"))
                    {
                        //    http://www.bookgew.com/Upload/Book/11028/201111201936011.gif
                        //   /Upload/Book/11028/201111201936011.gif
                        var hostName = ExtractDomainNameFromUrlMethod1(Metadata.ChapterUri.ToString());
                        
                        if (Metadata.Book.RootUrl == null )
                        {
                            Metadata.Book.RootUrl = String.Format("http://{0}/", hostName);
                        }

                        var rootUrl = Metadata.Book.RootUrl;
                        //var subUrl = url.StartsWith("/") ? url.Substring(1) : url;
                        //Process if image path is relative path
                        url = new Uri( new Uri(rootUrl), url).ToString();
                    }
                    urlList.Add(url);
                }
            }


            var imageList = new List<ArticleImage>();
            foreach (var imageUrl in urlList)
            {
                var imageSequence = 0;
                
                var image = new ArticleImage { ChapterId = Metadata.Id, SequenceId = imageSequence, ImageUrl = imageUrl};
                imageSequence++;
                imageList.Add(image);

                #region download image
                //var downloader = new HttpContentDownloader();
                //downloader.Download(new Uri(imageUrl, UriKind.Absolute), ar =>
                //{
                //    try
                //    {
                //        //At this step, we can get the index page in the search engine 
                //        var state = (RequestState)ar.AsyncState;
                //        var response = (HttpWebResponse)state.Request.EndGetResponse(ar);
                //        var imageStream = response.GetResponseStream();
                //        //Metadata.IsImageContent = true;
                        
                //         image.ImageUrl =  response.ResponseUri.ToString();
                //        //var image = new ArticleImage();

                //        var gd = new GifDecoder();
                //        var img = new ImageTools.ExtendedImage();

                //        gd.Decode(img, imageStream);

                //        var png = new PngEncoder();
                        
                //        using (var memStream = new MemoryStream())
                //        {
                //            png.Encode(img, memStream);
                //            byte[] byteArray = memStream.GetBuffer();
                //            image.ImageBytes = byteArray;
                //        }

                //        imageList.Add(image);
                        
                //        if (imageList.Count == urlList.Count)
                //        {
                //            PhoneStorage.GetPhoneStorageInstance().SaveArticleImages(imageList);
                //            Metadata.Downloaded = true;

                //            if (ParsingCompleted != null )
                //            {
                //                ParsingCompleted(this, null );
                //            }
                //        }
                        
                //    }
                //    catch (WebException ex)
                //    {
                //        throw ex;
                //    }
                //});

                #endregion download image
                if (imageList.Count == urlList.Count)
                {
                    PhoneStorage.GetPhoneStorageInstance().SaveArticleImages(imageList);
                    Metadata.Downloaded = true;

                    if (ParsingCompleted != null)
                    {
                        ParsingCompleted(this, null);
                    }
                } 
            }
        }