Beispiel #1
0
        private InlineUIContainer RenderImage(HTML.Element e, ParagraphSizeTracker pst)
        {
            Uri ImageURI;

            if (e.Attributes.ContainsKey("src") &&
                Uri.TryCreate(e.Attributes["src"], UriKind.RelativeOrAbsolute, out ImageURI))
            {
                Image image = new Image()
                {
                    Stretch = Stretch.Fill
                };
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = ImageURI;
                bitmapImage.EndInit();
                image.Source = bitmapImage;
                if (ImageSizeCache.ContainsKey(ImageURI))
                {
                    Tuple <int, int> size = ImageSizeCache[ImageURI];
                    image.Width  = size.Item1;
                    image.Height = size.Item2;
                }
                // This hack forces image size to match pixel size,
                // which undermines WPF's preference for honoring the
                // embedded DPI value which leads to upscaled and
                // mildly blurry images where that doesn't match
                // the local display.  Very high resolution displays
                // might need another approach.
                bitmapImage.DownloadCompleted += (s, ee) =>
                {
                    ImageSizeCache[ImageURI] = new Tuple <int, int>(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
                    image.Width  = bitmapImage.PixelWidth;
                    image.Height = bitmapImage.PixelHeight;
                };
                // Communicate image size info back up to the container
                if (pst != null)
                {
                    pst.Add(image);
                }
                if (e.Attributes.ContainsKey("title"))
                {
                    image.ToolTip = e.Attributes["title"];
                }
                else if (e.Attributes.ContainsKey("alt"))
                {
                    image.ToolTip = e.Attributes["alt"];
                }
                // TODO arrange to display alt value if image can't be loaded
                // (or before it has loaded)
                return(new InlineUIContainer()
                {
                    Child = image
                });
            }
            else
            {
                return(null);
            }
        }