public ColorChangeableCanvas() { this.InitializeComponent(); var watcher = new DependencyPropertyWatcher <Brush>(this, "Foreground"); watcher.PropertyChanged += (s, e) => { if (s is DependencyPropertyWatcher <Brush> w) { ForegroundPropertyChanged(w.Value); } }; win2DCanvas.CreateResources += (s, e) => { // s is CanvasControl // e is CanvasCreateResourcesEventArgs e.TrackAsyncAction(CreateResourcesAsync(s).AsAsyncAction()); }; win2DCanvas.Draw += (s, e) => { // s is CanvasControl // e is CanvasDrawEventArgs if (_bitmap is null) { return; } if (IsLoadInProgress()) { return; } // ビットマップにエフェクトを掛ける Color c = _foreground; //colorPicker.Color; float r = c.R / 255.0f; float g = c.G / 255.0f; float b = c.B / 255.0f; var colorMatrixEffect = new ColorMatrixEffect { Source = _bitmap, ColorMatrix = new Matrix5x4 { M11 = r, M12 = 0, M13 = 0, M14 = 0, M21 = 0, M22 = g, M23 = 0, M24 = 0, M31 = 0, M32 = 0, M33 = b, M34 = 0, M41 = 0, M42 = 0, M43 = 0, M44 = 1, M51 = 0, M52 = 0, M53 = 0, M54 = 0 }, }; // エフェクトを掛けた結果を表示する e.DrawingSession.DrawImage(colorMatrixEffect, 0, 0); // ※ ここでは、Win2D Canvas を UserControl の中に入れているため、表示サイズの調整が必要。 this.Width = _bitmap.Size.Width; this.Height = _bitmap.Size.Height; }; }
public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args) { DependencyPropertyWatcher <T> source = (DependencyPropertyWatcher <T>)sender; source.PropertyChanged?.Invoke(source, EventArgs.Empty); }