Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<MauiFont Include="Resources\Fonts\**" />
<MauiFont Remove="Resources\Fonts\Dokdo-Regular.ttf" />
<EmbeddedResource Include="Resources\Fonts\Dokdo-Regular.ttf" />
<EmbeddedResource Include="Resources\Images\royals.png" />
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

Expand Down
66 changes: 66 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18430.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Reflection;
using Microsoft.Maui.Graphics.Platform;
using IImage = Microsoft.Maui.Graphics.IImage;

namespace Controls.TestCases.HostApp.Issues;

[Issue(IssueTracker.Github, 18430, "CanvasDrawingSession Exception caused on Windows", PlatformAffected.UWP)]
public class Issue18430 : ContentPage
{
public Issue18430()
{
var label = new Label
{
Text = "Test should pass only if no exception is thrown and the image should clipped",
AutomationId = "Issue18430DescriptionLabel",

};

var graphicsView = new GraphicsView
{
HeightRequest = 300,
WidthRequest = 400,
Drawable = new Issue18430ClippingDrawable()
};

var layout = new VerticalStackLayout
{
Children =
{
label,
graphicsView
}
};

Content = new ScrollView { Content = layout };
}
}

public class Issue18430ClippingDrawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
IImage image;
var assembly = GetType().GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png"))
{
image = PlatformImage.FromStream(stream);
}

if (image != null)
{
float imageX = 10;
float imageY = 10;

float circleCenterX = imageX + image.Width / 2;
float circleCenterY = imageY + image.Height / 2;
float radius = Math.Min(image.Width, image.Height) / 2;

PathF path = new PathF();
path.AppendCircle(circleCenterX, circleCenterY, radius);

canvas.ClipPath(path);
canvas.DrawImage(image, imageX, imageY, image.Width, image.Height);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue18430 : _IssuesUITest
{
public Issue18430(TestDevice device) : base(device)
{
}

public override string Issue => "CanvasDrawingSession Exception caused on Windows";

[Test]
[Category(UITestCategories.GraphicsView)]
public void Issue18430ExceptionShouldNotThrown()
{
App.WaitForElement("Issue18430DescriptionLabel");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
_canvas.Session = args.DrawingSession;
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height);
_drawable.Draw(_canvas, _dirty);
_canvas.ResetState();
PlatformGraphicsService.ThreadLocalCreator = null;
Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping the draw logic (setting session, size, and calling Draw) and the ResetState call in a try/finally block so that the canvas state is always reset even if _drawable.Draw(...) throws an exception.

Suggested change
_canvas.Session = args.DrawingSession;
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height);
_drawable.Draw(_canvas, _dirty);
_canvas.ResetState();
PlatformGraphicsService.ThreadLocalCreator = null;
try
{
_canvas.Session = args.DrawingSession;
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height);
_drawable.Draw(_canvas, _dirty);
}
finally
{
_canvas.ResetState();
PlatformGraphicsService.ThreadLocalCreator = null;
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the suggestion is correct.

}
}
Expand Down