private DrawingImage RenderLayers() { var renderLayers = GetRenderLayers(); // Draw the current frame to the preview var keyboardRect = _deviceManager.ActiveKeyboard.KeyboardRectangle(); var visual = new DrawingVisual(); using (var drawingContext = visual.RenderOpen()) { // Setup the DrawingVisual's size drawingContext.PushClip(new RectangleGeometry(keyboardRect)); drawingContext.DrawRectangle(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)), null, keyboardRect); // Draw the layers foreach (var layer in renderLayers) { layer.Update(null, true, false); if (layer.LayerType.ShowInEdtor) { layer.Draw(null, drawingContext, true, false); } } // Get the selection color var accentColor = ThemeManager.DetectAppStyle(Application.Current)?.Item2?.Resources["AccentColor"]; if (accentColor == null) { var preview = new DrawingImage(); preview.Freeze(); KeyboardPreview = preview; return(new DrawingImage()); } var pen = new Pen(new SolidColorBrush((Color)accentColor), 0.4); // Draw the selection outline and resize indicator if (SelectedLayer != null && SelectedLayer.MustDraw()) { var layerRect = SelectedLayer.Properties.PropertiesRect(); // Deflate the rect so that the border is drawn on the inside layerRect.Inflate(-0.2, -0.2); // Draw an outline around the selected layer drawingContext.DrawRectangle(null, pen, layerRect); // Draw a resize indicator in the bottom-right drawingContext.DrawLine(pen, new Point(layerRect.BottomRight.X - 1, layerRect.BottomRight.Y - 0.5), new Point(layerRect.BottomRight.X - 1.2, layerRect.BottomRight.Y - 0.7)); drawingContext.DrawLine(pen, new Point(layerRect.BottomRight.X - 0.5, layerRect.BottomRight.Y - 1), new Point(layerRect.BottomRight.X - 0.7, layerRect.BottomRight.Y - 1.2)); drawingContext.DrawLine(pen, new Point(layerRect.BottomRight.X - 0.5, layerRect.BottomRight.Y - 0.5), new Point(layerRect.BottomRight.X - 0.7, layerRect.BottomRight.Y - 0.7)); } SelectedProfile?.RaiseDeviceDrawnEvent(new ProfileDeviceEventsArg(DrawType.Preview, null, true, drawingContext)); // Remove the clip drawingContext.Pop(); } var drawnPreview = new DrawingImage(visual.Drawing); drawnPreview.Freeze(); // Setup layers for the next frame if (_moduleModel.IsInitialized && ActiveWindowHelper.MainWindowActive) { _moduleModel.PreviewLayers = renderLayers; } return(drawnPreview); }