Skip to content

Commit 1e1085b

Browse files
SubhikshaSf4851PureWeen
authored andcommitted
[Windows] Fixed CanvasDrawingSession Exception on Clipping Image (#30028)
* [Windows] Fixed CanvasDrawingSession Exception * Update in description label * Updated Suggested Changes
1 parent f6b4dea commit 1e1085b

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Reflection;
2+
using Microsoft.Maui.Graphics.Platform;
3+
using IImage = Microsoft.Maui.Graphics.IImage;
4+
5+
namespace Controls.TestCases.HostApp.Issues;
6+
7+
[Issue(IssueTracker.Github, 18430, "CanvasDrawingSession Exception caused on Windows", PlatformAffected.UWP)]
8+
public class Issue18430 : ContentPage
9+
{
10+
public Issue18430()
11+
{
12+
var label = new Label
13+
{
14+
Text = "Test should pass only if no exception is thrown and the image should clipped",
15+
AutomationId = "Issue18430DescriptionLabel",
16+
17+
};
18+
19+
var graphicsView = new GraphicsView
20+
{
21+
HeightRequest = 300,
22+
WidthRequest = 400,
23+
Drawable = new Issue18430ClippingDrawable()
24+
};
25+
26+
var layout = new VerticalStackLayout
27+
{
28+
Children =
29+
{
30+
label,
31+
graphicsView
32+
}
33+
};
34+
35+
Content = new ScrollView { Content = layout };
36+
}
37+
}
38+
39+
public class Issue18430ClippingDrawable : IDrawable
40+
{
41+
public void Draw(ICanvas canvas, RectF dirtyRect)
42+
{
43+
IImage image;
44+
var assembly = GetType().GetTypeInfo().Assembly;
45+
using (var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png"))
46+
{
47+
image = PlatformImage.FromStream(stream);
48+
}
49+
50+
if (image != null)
51+
{
52+
float imageX = 10;
53+
float imageY = 10;
54+
55+
float circleCenterX = imageX + image.Width / 2;
56+
float circleCenterY = imageY + image.Height / 2;
57+
float radius = Math.Min(image.Width, image.Height) / 2;
58+
59+
PathF path = new PathF();
60+
path.AppendCircle(circleCenterX, circleCenterY, radius);
61+
62+
canvas.ClipPath(path);
63+
canvas.DrawImage(image, imageX, imageY, image.Width, image.Height);
64+
}
65+
}
66+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue18430 : _IssuesUITest
8+
{
9+
public Issue18430(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "CanvasDrawingSession Exception caused on Windows";
14+
15+
[Test]
16+
[Category(UITestCategories.GraphicsView)]
17+
public void Issue18430ExceptionShouldNotThrown()
18+
{
19+
App.WaitForElement("Issue18430DescriptionLabel");
20+
}
21+
}

src/Graphics/src/Graphics/Platforms/Windows/PlatformGraphicsView.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,17 @@ private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
8585
_dirty.Height = (float)sender.ActualHeight;
8686

8787
PlatformGraphicsService.ThreadLocalCreator = sender;
88-
_canvas.Session = args.DrawingSession;
89-
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height);
90-
_drawable.Draw(_canvas, _dirty);
91-
PlatformGraphicsService.ThreadLocalCreator = null;
88+
try
89+
{
90+
_canvas.Session = args.DrawingSession;
91+
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height);
92+
_drawable.Draw(_canvas, _dirty);
93+
}
94+
finally
95+
{
96+
_canvas.ResetState();
97+
PlatformGraphicsService.ThreadLocalCreator = null;
98+
}
9299
}
93100
}
94101
}

0 commit comments

Comments
 (0)