private static void AddCachedImage(Canvas target, string imageId, IEnumerable <string> imageBoxOverrides = null) { if (TryGetCachedImage(imageId, out CachedImage cachedImage)) { ImageBoxDescriptor imageBoxDescriptor = null; if (null == imageBoxOverrides) { var defaultOverrides = new List <string>(); string[] split = imageId.Split('.', StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < split.Length; i++) { defaultOverrides.Add(string.Join('.', split.Skip(i).ToArray())); } imageBoxOverrides = defaultOverrides; } foreach (var imageBoxId in imageBoxOverrides) { if (CurrentConfig.ImageBoxDescriptors.TryGetValue(imageBoxId, out imageBoxDescriptor)) { break; } } target.Children.Add(cachedImage.ToControl(imageBoxDescriptor)); } }
public IControl ToControl(ImageBoxDescriptor imageBoxDescriptor = null) { var control = new Image() { Source = Bitmap }; if (null == imageBoxDescriptor) { control.SetValue(Canvas.LeftProperty, X); control.SetValue(Canvas.TopProperty, Y); } if (null != imageBoxDescriptor) { control.Width = imageBoxDescriptor.Width; control.Height = imageBoxDescriptor.Height; control.Stretch = Stretch.Uniform; control.SetValue(Canvas.LeftProperty, imageBoxDescriptor.X); control.SetValue(Canvas.TopProperty, imageBoxDescriptor.Y); } return(control); }
private static bool TryCreateImage(string base64, IEnumerable <string> imageBoxOverrides, out IControl result) { ImageBoxDescriptor imageBoxDescriptor = null; if (null != imageBoxOverrides) { foreach (var imageBoxId in imageBoxOverrides) { if (CurrentConfig.ImageBoxDescriptors.TryGetValue(imageBoxId, out imageBoxDescriptor)) { break; } } } if (null == imageBoxDescriptor) { result = null; return(false); } var border = new Border() { Width = imageBoxDescriptor.Width, Height = imageBoxDescriptor.Height, }; if (AppVM.DebugMode) { border.Background = Brushes.Magenta; } Canvas.SetLeft(border, imageBoxDescriptor.X); Canvas.SetTop(border, imageBoxDescriptor.Y); try { var stream = Base64Utils.CreateStreamFromBase64(base64); Image image = new Image() { Source = new Bitmap(stream), Width = imageBoxDescriptor.Width, Height = imageBoxDescriptor.Height, Stretch = Stretch.UniformToFill, }; border.Child = image; } catch (Exception) { } result = border; return(true); }