-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6014 from AvaloniaUI/feature/imagedrawing
Implemented ImageDrawing.
- Loading branch information
1 parent
e3498f4
commit 9123a54
Showing
8 changed files
with
137 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#nullable enable | ||
|
||
namespace Avalonia.Media | ||
{ | ||
/// <summary> | ||
/// Draws an image within a region defined by a <see cref="Rect"/>. | ||
/// </summary> | ||
public class ImageDrawing : Drawing | ||
{ | ||
/// <summary> | ||
/// Defines the <see cref="ImageSource"/> property. | ||
/// </summary> | ||
public static readonly StyledProperty<IImage?> ImageSourceProperty = | ||
AvaloniaProperty.Register<ImageDrawing, IImage?>(nameof(ImageSource)); | ||
|
||
/// <summary> | ||
/// Defines the <see cref="Rect"/> property. | ||
/// </summary> | ||
public static readonly StyledProperty<Rect> RectProperty = | ||
AvaloniaProperty.Register<ImageDrawing, Rect>(nameof(Rect)); | ||
|
||
/// <summary> | ||
/// Gets or sets the source of the image. | ||
/// </summary> | ||
public IImage? ImageSource | ||
{ | ||
get => GetValue(ImageSourceProperty); | ||
set => SetValue(ImageSourceProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets region in which the image is drawn. | ||
/// </summary> | ||
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; | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
Binary file added
BIN
+3.44 KB
tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.3 KB
tests/TestFiles/Direct2D1/Media/ImageDrawing/ImageDrawing_Fill.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.11 KB
tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_BottomRight.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.74 KB
tests/TestFiles/Skia/Media/ImageDrawing/ImageDrawing_Fill.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.