diff --git a/src/Avalonia.Visuals/Media/ImageDrawing.cs b/src/Avalonia.Visuals/Media/ImageDrawing.cs new file mode 100644 index 00000000000..82f97b52b41 --- /dev/null +++ b/src/Avalonia.Visuals/Media/ImageDrawing.cs @@ -0,0 +1,53 @@ +#nullable enable + +namespace Avalonia.Media +{ + /// + /// Draws an image within a region defined by a . + /// + public class ImageDrawing : Drawing + { + /// + /// Defines the property. + /// + public static readonly StyledProperty ImageSourceProperty = + AvaloniaProperty.Register(nameof(ImageSource)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty RectProperty = + AvaloniaProperty.Register(nameof(Rect)); + + /// + /// Gets or sets the source of the image. + /// + public IImage? ImageSource + { + get => GetValue(ImageSourceProperty); + set => SetValue(ImageSourceProperty, value); + } + + /// + /// Gets or sets region in which the image is drawn. + /// + public Rect Rect + { + get => GetValue(RectProperty); + set => SetValue(RectProperty, value); + } + + public override void Draw(DrawingContext context) + { + var imageSource = ImageSource; + var rect = Rect; + + if (imageSource is object && !rect.IsEmpty) + { + context.DrawImage(imageSource, rect); + } + } + + public override Rect GetBounds() => Rect; + } +} diff --git a/tests/Avalonia.RenderTests/Media/ImageDrawingTests.cs b/tests/Avalonia.RenderTests/Media/ImageDrawingTests.cs new file mode 100644 index 00000000000..5ca21a35352 --- /dev/null +++ b/tests/Avalonia.RenderTests/Media/ImageDrawingTests.cs @@ -0,0 +1,84 @@ +using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Xunit; + +#if AVALONIA_SKIA +namespace Avalonia.Skia.RenderTests +#else +namespace Avalonia.Direct2D1.RenderTests.Media +#endif +{ + public class ImageDrawingTests : TestBase + { + public ImageDrawingTests() + : base(@"Media\ImageDrawing") + { + } + + private string BitmapPath + { + get { return System.IO.Path.Combine(OutputPath, "github_icon.png"); } + } + + [Fact] + public async Task ImageDrawing_Fill() + { + Decorator target = new Decorator + { + Width = 200, + Height = 200, + Child = new Image + { + Source = new DrawingImage + { + Drawing = new ImageDrawing + { + ImageSource = new Bitmap(BitmapPath), + Rect = new Rect(0, 0, 200, 200), + } + } + } + }; + + await RenderToFile(target); + CompareImages(); + } + + [Fact] + public async Task ImageDrawing_BottomRight() + { + Decorator target = new Decorator + { + Width = 200, + Height = 200, + Child = new Image + { + Source = new DrawingImage + { + Drawing = new DrawingGroup + { + Children = + { + new GeometryDrawing + { + Geometry = StreamGeometry.Parse("m0,0 l200,200"), + Brush = Brushes.Black, + }, + new ImageDrawing + { + ImageSource = new Bitmap(BitmapPath), + Rect = new Rect(100, 100, 100, 100), + } + } + } + } + } + }; + + await RenderToFile(target); + CompareImages(); + } + } +} diff --git a/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png b/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png new file mode 100644 index 00000000000..c6b7c2f3077 Binary files /dev/null and b/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png differ diff --git a/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_Fill.expected.png b/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_Fill.expected.png new file mode 100644 index 00000000000..acc8532ff98 Binary files /dev/null and b/tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_Fill.expected.png differ diff --git a/tests/TestFiles/Direct2D1/Media/ImageDrawing/github_icon.png b/tests/TestFiles/Direct2D1/Media/ImageDrawing/github_icon.png new file mode 100644 index 00000000000..cd053c5fe1e Binary files /dev/null and b/tests/TestFiles/Direct2D1/Media/ImageDrawing/github_icon.png differ diff --git a/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png b/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png new file mode 100644 index 00000000000..f7a25105519 Binary files /dev/null and b/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png differ diff --git a/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_Fill.expected.png b/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_Fill.expected.png new file mode 100644 index 00000000000..fe99db43aad Binary files /dev/null and b/tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_Fill.expected.png differ diff --git a/tests/TestFiles/Skia/Media/ImageDrawing/github_icon.png b/tests/TestFiles/Skia/Media/ImageDrawing/github_icon.png new file mode 100644 index 00000000000..cd053c5fe1e Binary files /dev/null and b/tests/TestFiles/Skia/Media/ImageDrawing/github_icon.png differ