示例#1
0
        /// <summary>
        /// Sets image source and size (by scale factor)
        /// </summary>
        /// <param name="scale">If > 0, scales the image by the specified amount, otherwise uses source image dimensions</param>
        /// <param name="exception">Is set to the Exception instance if image couldn't be loaded, otherwise null</param>
        /// <returns>Returns true if image was succesfully loaded, otherwise false</returns>
        public bool TrySet(string imageUrl, double scale, out Exception exception)
        {
            exception = null;
            try
            {
                //TODO [!]: Currently, this loading system prevents images from being changed on disk, fix this
                //  e.g. using http://stackoverflow.com/questions/1763608/display-an-image-in-wpf-without-holding-the-file-open
                Uri uri = new Uri(_variableExpander.ProcessText(imageUrl), UriKind.Absolute);

                if (uri.Scheme == "data")
                {
                    this.Source = BitmapFrame.Create(DataUriLoader.Load(uri));
                }
                else
                {
                    this.Source = BitmapFrame.Create(uri);
                }

                this.Url = imageUrl;
            }
            catch (Exception ex)
            {
                exception = ex;
                return(false);
            }
            this.Scale = scale;
            return(true);
        }
示例#2
0
        /// <summary>
        /// Sets image source and size (by scale factor)
        /// </summary>
        /// <param name="scale">If > 0, scales the image by the specified amount, otherwise uses source image dimensions</param>
        /// <param name="exception">Is set to the Exception instance if image couldn't be loaded, otherwise null</param>
        /// <returns>Returns true if image was succesfully loaded, otherwise false</returns>
        public bool TrySet(string imageUrl, double scale, out Exception exception, Action refreshAction)
        {
            // Remove old watcher.
            var watcher = _watcher;

            _watcher = null;
            watcher?.Dispose();
            // ---
            exception = null;
            try
            {
                var expandedUrl = _variableExpander.ProcessText(imageUrl);
                if (File.Exists(expandedUrl))
                {
                    var data = new MemoryStream(File.ReadAllBytes(expandedUrl));
                    Source = BitmapFrame.Create(data);
                    // Create file system watcher to update changed image file.
                    _watcher = new FileSystemWatcher
                    {
                        //NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size,
                        Path   = Path.GetDirectoryName(expandedUrl),
                        Filter = Path.GetFileName(expandedUrl)
                    };
                    var w = _watcher;
                    FileSystemEventHandler refresh = delegate
                    {
                        try
                        {
                            var enableRaisingEvents = w.EnableRaisingEvents;
                            w.EnableRaisingEvents = false;
                            if (enableRaisingEvents)
                            {
                                Url = null;
                                refreshAction();
                            }
                        }
                        catch { }
                    };
                    _watcher.Changed            += refresh;
                    _watcher.Renamed            += (s, a) => refresh(s, a);
                    _watcher.Deleted            += refresh;
                    _watcher.EnableRaisingEvents = true;
                }
                else
                {
                    //TODO [!]: Currently, this loading system prevents images from being changed on disk, fix this
                    //  e.g. using http://stackoverflow.com/questions/1763608/display-an-image-in-wpf-without-holding-the-file-open
                    this.Source = BitmapFrame.Create(new Uri(expandedUrl, UriKind.Absolute));
                }
                Url = imageUrl;
            }
            catch (Exception ex)
            {
                exception = ex;
                return(false);
            }
            this.Scale = scale;
            return(true);
        }
        private bool LoadFromUri(string rawUri, string sourceFileDir, Action refreshAction, out string errorString)
        {
            if (string.IsNullOrWhiteSpace(rawUri))
            {
                Source      = null;
                errorString = "No image specified";
                return(false);
            }

            var expandedUrl = _variableExpander.ProcessText(rawUri);

            if (!File.Exists(expandedUrl)) //TODO: Refactor this eg. post processing step
            {
                // if the file does not exists, but we have an existing "docfx.json", lets try to find file in "$(ProjectDir)\images" directory
                var jsonFile = _variableExpander.ProcessText("$(ProjectDir)\\docfx.json");
                if (File.Exists(jsonFile))
                {
                    // Example: we replace in "..\\images\picture.png" all the ".." with "$ProjectDir" --> "$ProjectDir\\images\\picture.png"
                    expandedUrl = rawUri.Replace("..", "$(ProjectDir)");
                    expandedUrl = _variableExpander.ProcessText(expandedUrl);
                }
            }

            var success        = Uri.TryCreate(_variableExpander.ProcessText(expandedUrl), UriKind.Absolute, out var uri);
            var canLoadData    = success && DataUriLoader.CanLoad(uri);
            var canLoadFromWeb = success && WebLoader.CanLoad(uri);

            if (canLoadData)
            {
                //TODO [!]: Currently, this loading system prevents images from being changed on disk, fix this
                //  e.g. using http://stackoverflow.com/questions/1763608/display-an-image-in-wpf-without-holding-the-file-open
                Source = Load(DataUriLoader.Load(uri), uri);
            }
            else if (canLoadFromWeb)
            {
                expandedUrl = WebLoader.Load(uri);
            }
            else if (!success && !Path.IsPathRooted(expandedUrl) && sourceFileDir != null)
            {
                expandedUrl = Path.Combine(sourceFileDir, expandedUrl);
                expandedUrl = Path.GetFullPath((new Uri(expandedUrl)).LocalPath);
            }

            if (!canLoadData && File.Exists(expandedUrl))
            {
                var data = new MemoryStream(File.ReadAllBytes(expandedUrl));
                Source = Load(data, new Uri(expandedUrl));
                // Create file system watcher to update changed image file.
                _watcher = new FileSystemWatcher
                {
                    //NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size,
                    Path   = Path.GetDirectoryName(expandedUrl),
                    Filter = Path.GetFileName(expandedUrl)
                };
                var w = _watcher;

                void Refresh(object sender, FileSystemEventArgs e)
                {
                    try
                    {
                        var enableRaisingEvents = w.EnableRaisingEvents;
                        w.EnableRaisingEvents = false;
                        if (!enableRaisingEvents)
                        {
                            return;
                        }

                        Attributes.Url = null;
                        refreshAction();
                    }
                    catch
                    {
                        // ignored
                    }
                }

                _watcher.Changed            += Refresh;
                _watcher.Renamed            += Refresh;
                _watcher.Deleted            += Refresh;
                _watcher.EnableRaisingEvents = true;

                errorString = null;
                return(true);
            }

            Source      = null;
            errorString = $"Could not load image '{uri}' (resolved to '{expandedUrl}')";
            return(false);
        }
示例#4
0
        /// <summary>
        /// Sets image source and size (by scale factor)
        /// </summary>
        /// <param name="scale">If > 0, scales the image by the specified amount, otherwise uses source image dimensions</param>
        /// <param name="exception">Is set to the Exception instance if image couldn't be loaded, otherwise null</param>
        /// <returns>Returns true if image was successfully loaded, otherwise false</returns>
        public bool TrySet(string imageUrl, double scale, Color bgColor, out Exception exception, Action refreshAction)
        {
            // Remove old watcher.
            var watcher = _watcher;

            _watcher = null;
            if (watcher != null)
            {
                watcher.Dispose();
            }
            // ---
            exception = null;
            try
            {
                var expandedUrl = _variableExpander.ProcessText(imageUrl);
                if (!File.Exists(expandedUrl))
                {
                    // if the file does not exists, but we have an existing "docfx.json", lets try to find file in "$(ProjectDir)\images" directory
                    var jsonFile = _variableExpander.ProcessText("$(ProjectDir)\\docfx.json");
                    if (File.Exists(jsonFile))
                    {
                        // Example: we replace in "..\\images\picture.png" all the ".." with "$ProjectDir" --> "$ProjectDir\\images\\picture.png"
                        imageUrl    = imageUrl.Replace("..", "$(ProjectDir)");
                        expandedUrl = _variableExpander.ProcessText(imageUrl);
                    }
                }

                if (File.Exists(expandedUrl))
                {
                    var data = new MemoryStream(File.ReadAllBytes(expandedUrl));
                    Source = BitmapFrame.Create(data);
                    // Create file system watcher to update changed image file.
                    _watcher = new FileSystemWatcher
                    {
                        //NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size,
                        Path   = Path.GetDirectoryName(expandedUrl),
                        Filter = Path.GetFileName(expandedUrl)
                    };
                    var w = _watcher;
                    FileSystemEventHandler refresh = delegate
                    {
                        try
                        {
                            var enableRaisingEvents = w.EnableRaisingEvents;
                            w.EnableRaisingEvents = false;
                            if (enableRaisingEvents)
                            {
                                Url = null;
                                refreshAction();
                            }
                        }
                        catch { }
                    };
                    _watcher.Changed            += refresh;
                    _watcher.Renamed            += (s, a) => refresh(s, a);
                    _watcher.Deleted            += refresh;
                    _watcher.EnableRaisingEvents = true;
                }
                else
                {
                    //TODO [!]: Currently, this loading system prevents images from being changed on disk, fix this
                    //  e.g. using http://stackoverflow.com/questions/1763608/display-an-image-in-wpf-without-holding-the-file-open
                    Uri uri = new Uri(_variableExpander.ProcessText(expandedUrl), UriKind.Absolute);

                    if (uri.Scheme == "data")
                    {
                        Source = BitmapFrame.Create(DataUriLoader.Load(uri));
                    }
                    else
                    {
                        Source = BitmapFrame.Create(uri);
                    }
                }

                if (bgColor.A != 0)
                {
                    Source  = ReplaceTransparency(Source, bgColor);
                    BgColor = bgColor;
                }

                Url = imageUrl;
            }
            catch (Exception ex)
            {
                exception = ex;
                return(false);
            }
            this.Scale = scale;
            return(true);
        }