Esempio n. 1
0
        private void ParseImg(HtmlNode htmlNode, ref List <Elements.BaseElement> elements)
        {
            string path = _ePubViewer.Book.GetFilePath(HttpUtility.UrlDecode(htmlNode.Attributes["src"].Value.Replace("../", "")));

            if (path == null) // img src file does not exists (don't render)
            {
                Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Image \"{0}\" does not exists.", HttpUtility.UrlDecode(htmlNode.Attributes["src"].Value.Replace("../", ""))));
                return;
            }

            Stream s = IsolatedStorageHelper.ReadZipStream(path);

            string fileName = Guid.NewGuid().ToString();
            Size   size     = Size.Empty;

            // detect image type
            if (
                path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jpe", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jfif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jfi", StringComparison.OrdinalIgnoreCase)
                )
            {
                BitmapSource bs = new BitmapImage();
                bs.SetSource(s);

                size = new Size(bs.PixelWidth, bs.PixelHeight);

                // save image
                fileName += ".jpg";
                string isfPath = Path.Combine(_ePubViewer.Book.GetImagesDirectory(), fileName);
                try
                {
                    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isf.DirectoryExists(Path.GetDirectoryName(isfPath)))
                        {
                            isf.CreateDirectory(Path.GetDirectoryName(isfPath));
                        }

                        using (IsolatedStorageFileStream isfs = isf.CreateFile(isfPath))
                        {
                            WriteableBitmap wb = new WriteableBitmap(bs);
                            wb.SaveJpeg(isfs, bs.PixelWidth, bs.PixelHeight, 0, 100);
                            isfs.Close();
                        }
                    }
                }
                catch
                {
                    Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Error saving image \"{0}\".", isfPath));
                    return;
                }
            }
            else if (
                path.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".dib", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
                )
            {
                ImageTools.ExtendedImage    extendedImage = new ImageTools.ExtendedImage();
                ImageTools.IO.IImageDecoder decoder;

                if (
                    path.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
                    ||
                    path.EndsWith(".dib", StringComparison.OrdinalIgnoreCase)
                    )
                {
                    decoder = new ImageTools.IO.Bmp.BmpDecoder();
                }
                else if (path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                {
                    decoder = new ImageTools.IO.Gif.GifDecoder();
                }
                else // if (path.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
                {
                    decoder = new ImageTools.IO.Png.PngDecoder();
                }

                decoder.Decode(extendedImage, s);

                size = new Size(extendedImage.PixelWidth, extendedImage.PixelHeight);

                ImageTools.IO.Png.PngEncoder pngEncoder = new ImageTools.IO.Png.PngEncoder();

                // save image
                fileName += ".png";
                string isfPath = Path.Combine(_ePubViewer.Book.GetImagesDirectory(), fileName);
                try
                {
                    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isf.DirectoryExists(Path.GetDirectoryName(isfPath)))
                        {
                            isf.CreateDirectory(Path.GetDirectoryName(isfPath));
                        }

                        using (IsolatedStorageFileStream isfs = isf.CreateFile(isfPath))
                        {
                            pngEncoder.Encode(extendedImage, isfs);
                            isfs.Close();
                        }
                    }
                }
                catch
                {
                    Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Error saving image \"{0}\".", isfPath));
                    return;
                }
            }
            else
            {
                s.Close();
                s.Dispose();

                Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Not recognizable image type \"{0}\".", path));
                return;
            }

            RemoveLastBreak(ref elements);

            // add image
            Elements.BaseElement element = new Elements.Image()
            {
                FileName = fileName,
                Width    = (int)size.Width,
                Height   = (int)size.Height
            };

            ExtractId(htmlNode, ref element);

            elements.Add(element);
        }
Esempio n. 2
0
        private void RenderImage(ItemElementsContainer iec, int currentLocation, ref Page page, ref double top, Elements.BaseElement element)
        {
            Elements.Image imageElement = (Elements.Image)element;

            // create UI element
            Image image = new Image();

            image.Tag = imageElement;

            Binding binding = new Binding("Tag");

            binding.RelativeSource     = new RelativeSource(RelativeSourceMode.Self);
            binding.Converter          = new Converters.IsolatedStorageFileToImageSourceConverter();
            binding.ConverterParameter = _ePubViewer.Book;
            BindingOperations.SetBinding(image, Image.SourceProperty, binding);

            // check size of image (it it's larger then entire space)
            if (imageElement.Width + image.Margin.Left + image.Margin.Right > page.Content.Width
                ||
                imageElement.Height + image.Margin.Top + image.Margin.Bottom > page.Content.Height)
            {
                // image bigger then size of control
                if (page.Content.Children.Count > 0)
                {
                    PushPage(iec, ref page, currentLocation, ref top);
                }

                // count new width / height
                double widthRatio  = (double)imageElement.Width / page.Content.Width;
                double heightRatio = (double)imageElement.Height / page.Content.Height;
                double ratio       = Math.Max(widthRatio, heightRatio);
                image.Width  = (int)((double)imageElement.Width / ratio);
                image.Height = (int)((double)imageElement.Height / ratio);

                image.Stretch = Stretch.Uniform;

                image.SetValue(Canvas.TopProperty, top);

                page.Content.Children.Add(image);
            }
            else
            {
                // image smaller then size of control

                // try fit current page
                if (top + imageElement.Height + image.Margin.Top + image.Margin.Bottom > page.Content.Height)
                {
                    PushPage(iec, ref page, currentLocation, ref top);
                }

                image.Width  = imageElement.Width;
                image.Height = imageElement.Height;

                image.Stretch = Stretch.None;

                image.SetValue(Canvas.TopProperty, top);

                page.Content.Children.Add(image);
            }

            top += image.Height + image.Margin.Top + image.Margin.Bottom;
        }