-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add white background for png image. (#367)
- Loading branch information
1 parent
22d38e9
commit bf3e5d5
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Media; | ||
using System.Windows.Shapes; | ||
|
||
namespace CompMs.Graphics.IO; | ||
|
||
public sealed class SetBackgroundConverter : DependencyObject, IVisualConverter | ||
{ | ||
public static readonly DependencyProperty BackgroundProperty = | ||
DependencyProperty.Register( | ||
nameof(Background), | ||
typeof(Brush), | ||
typeof(SetBackgroundConverter), | ||
new PropertyMetadata(Brushes.Transparent)); | ||
|
||
public Brush Background { | ||
get => (Brush)GetValue(BackgroundProperty); | ||
set => SetValue(BackgroundProperty, value); | ||
} | ||
|
||
public FrameworkElement Convert(FrameworkElement element) { | ||
var brush = new VisualBrush(element); | ||
var width = element.ActualWidth; | ||
var height = element.ActualHeight; | ||
var canvas = new Canvas | ||
{ | ||
Width = width, | ||
Height = height, | ||
Background = Background, | ||
}; | ||
|
||
var rectangle = new Rectangle | ||
{ | ||
Width = width, | ||
Height = height, | ||
Fill = brush, | ||
}; | ||
Canvas.SetTop(rectangle, 0d); | ||
Canvas.SetLeft(rectangle, 0d); | ||
|
||
canvas.Children.Add(rectangle); | ||
canvas.Measure(new Size(width, height)); | ||
canvas.Arrange(new Rect(0, 0, width, height)); | ||
|
||
return canvas; | ||
} | ||
|
||
public static SetBackgroundConverter White { get; } = new SetBackgroundConverter { Background = Brushes.White }; | ||
} |