private static async void SourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { if (e.OldValue is string oldFilePath && PreviewedFilePaths.Contains(oldFilePath)) { PreviewedFilePaths.Remove(oldFilePath); } if (obj is Image image) { if (e.NewValue is string filePath && !String.IsNullOrEmpty(filePath)) { FileInfo file = new FileInfo(filePath); if (file.Exists && SupportedExtensions.Any(x => x.OrdinalEquals(file.Extension))) { try { image.Tag = filePath; PreviewedFilePaths.Add(filePath); int hashCode = image.GetHashCode(); if (!ImageReferences.ContainsKey(hashCode)) { ImageReferences.Add(hashCode, new WeakReference <Image>(image)); image.Unloaded += (x, y) => { PreviewedFilePaths.Remove(image.Tag?.ToString()); }; } if (file.Length > 1048576) { image.Visibility = Visibility.Collapsed; } image.Tag = file.FullName; image.Source = await ReadImageAsync(file.FullName); } finally { image.Visibility = Visibility.Visible; } } } else { image.Tag = null; image.Source = null; } }
private void ParseBodyForImageReferences() { if (string.IsNullOrEmpty(Body?.Value)) { return; } var doc = new HtmlDocument(); try { doc.LoadHtml(Body.Value); var nodes = doc?.DocumentNode?.SelectNodes("//img"); if (nodes != null) { foreach (var node in nodes) { var src = node.GetAttributeValue("src", null); if (!string.IsNullOrEmpty(src) && src.Contains("-/media")) { var mediaStripped = src.Replace("-/media/", string.Empty); var mediaId = mediaStripped.Substring(0, mediaStripped.IndexOf('.')); if (Guid.TryParse(mediaId, out var mediaGuid)) { ImageReferences.Add(mediaGuid.ToString("B").ToUpper()); } } } } } catch (Exception e) { Console.WriteLine(e); } }