Skip to content

[Windows] Fixed CanvasDrawingSession Exception on Clipping Image #30028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SubhikshaSf4851
Copy link
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root Cause

The exception CanvasDrawingSessionException occurred because the Canvas state was not reset after drawing

Description of Change

Added a call to _canvas.ResetState() in the OnDraw method to ensure the canvas state is properly reset after drawing.

Tested the behavior in the following platforms:

  • Windows
  • Android
  • iOS
  • Mac

Issues Fixed

Fixes #18430

Screenshot

Before Issue Fix After Issue Fix

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels Jun 17, 2025
@SubhikshaSf4851 SubhikshaSf4851 marked this pull request as ready for review June 18, 2025 03:43
@Copilot Copilot AI review requested due to automatic review settings June 18, 2025 03:43
@SubhikshaSf4851 SubhikshaSf4851 requested a review from a team as a code owner June 18, 2025 03:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a Windows-only CanvasDrawingSessionException by resetting the canvas state after each draw, and adds a full repro via HostApp and automated tests.

  • Adds _canvas.ResetState() in OnDraw to ensure the canvas is cleared after drawing.
  • Introduces a new UI page (Issue18430) with clipping drawable and embeds the required image resource.
  • Adds shared and host-app test cases to validate that no exception is thrown.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/Graphics/src/Graphics/Platforms/Windows/PlatformGraphicsView.cs Call ResetState() after drawing to clear state
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18430.cs New shared UI test checking that the page loads
src/Controls/tests/TestCases.HostApp/Issues/Issue18430.cs HostApp page and clipping drawable for repro
src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj Embed royals.png so the drawable can load the image

Comment on lines 88 to 92
_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
Preview

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.

@sheiksyedm sheiksyedm added platform/windows area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing labels Jun 18, 2025
Comment on lines 88 to 92
_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
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.

@SubhikshaSf4851
Copy link
Contributor Author

@jsuarezruiz I have modified the suggested changes

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[regression/8.0.0] Clipping not working on Windows
3 participants