Skip to content

Add DrawingImage.Viewbox#18913

Merged
MrJul merged 3 commits into
AvaloniaUI:masterfrom
Enscape:feature/drawingimage-frame
Jan 6, 2026
Merged

Add DrawingImage.Viewbox#18913
MrJul merged 3 commits into
AvaloniaUI:masterfrom
Enscape:feature/drawingimage-frame

Conversation

@TomEdwardsEnscape

@TomEdwardsEnscape TomEdwardsEnscape commented May 24, 2025

Copy link
Copy Markdown
Contributor

This is a new property which gets or sets a rectangular region of Drawing, in device independent pixels, to display when rendering the image.

The value can be used to display only part of Drawing, or to surround it with empty space. If null, Drawing will provide its own frame, the same as before.

What is the current behavior?

The drawing is framed with a tight bounding box defined by a union of the bounds of each component of the drawing. Expanding the frame requires adding invisible drawing elements. It's not possible to reduce the size of the frame, only to crop the rasterised output later.

What is the updated/expected behavior with this PR?

The following XAML (which is the test case of #18871, plus Viewbox and a border) now produces the pictured output. The arc in the drawing is framed with enough space for the entire circle, because a frame starting at 0,0 has been defined.

<Border BorderBrush="Green" BorderThickness="2" VerticalAlignment="Center" HorizontalAlignment="Center">
  <Image>
    <DrawingImage Viewbox="0 0 32 32">
      <GeometryDrawing>
        <GeometryDrawing.Pen>
          <Pen Brush="Black" LineJoin="Miter" Thickness="2.0"/>
        </GeometryDrawing.Pen>
        <GeometryDrawing.Geometry>
          <PathGeometry Figures="M 31.082 16 A 15.082 15.0815 0 0 1 25.8947 27.3822" FillRule="NonZero" />
        </GeometryDrawing.Geometry>
      </GeometryDrawing>
    </DrawingImage>
  </Image>
</Border>
{26D833E1-39A7-498E-875F-BD563B2CB415}

How was the solution implemented (if it's not obvious)?

At first I added the property to GeometryGroup per the bug reports, but then I realised that if I used DrawingGroup to add an image to the drawing, the geometry began to draw at the intended position. The problem was higher up the tree, with DrawingImage itself, which automatically crops the image to the bounds of the drawing. It still does, but now you can override those bounds and have them start and end wherever you like.

I wanted to use the equivalent SVG attribute name for the property, but unfortunately ViewBox is already the name of an Avalonia control. I also decided against "bounds", because the frame is explicitly NOT the bounds of the drawing. That name is also already in use with Visual.Bounds, a read-only property, which could become a source of confusion. Viewbox is now used at the request of the Avalonia team.

Breaking changes

None.

Obsoletions / Deprecations

None.

Fixed issues

Fixes #18871
Fixes #18512

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.0.999-cibuild0056671-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@emmauss emmauss added the needs-api-review The PR adds new public APIs that should be reviewed. label May 27, 2025
@MrJul MrJul added the feature label May 31, 2025
…e independent pixels, to display when rendering the image
@TomEdwardsEnscape TomEdwardsEnscape force-pushed the feature/drawingimage-frame branch from 1a8db7c to e69182d Compare November 23, 2025 15:04
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.0.999-cibuild0060277-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added api-change-requested The new public APIs need some changes. and removed needs-api-review The PR adds new public APIs that should be reviewed. labels Dec 16, 2025
@MrJul

MrJul commented Dec 16, 2025

Copy link
Copy Markdown
Member

Notes from the API review: rename Frame to Viewbox.
Also, unrelated to the API itself, please add one or two render tests, plus a test with the drawing as the source of a DrawingBrush.

@TomEdwardsEnscape

Copy link
Copy Markdown
Contributor Author

I wasn't able to extend the feature to brushes. I can clip and translate the output, but this doesn't alter tiling. It looks like doing so would require low-level changes to the rendering system.

@TomEdwardsEnscape TomEdwardsEnscape changed the title Add DrawingImage.Frame Add DrawingImage.Viewbox Jan 1, 2026
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.0.999-cibuild0061183-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

@MrJul MrJul added this pull request to the merge queue Jan 6, 2026
Merged via the queue into AvaloniaUI:master with commit f1703dd Jan 6, 2026
11 checks passed
@TomEdwardsEnscape TomEdwardsEnscape deleted the feature/drawingimage-frame branch January 6, 2026 19:56
@MrJul MrJul added api-approved The new public APIs have been approved. and removed api-change-requested The new public APIs need some changes. labels Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-approved The new public APIs have been approved. feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support retaining whitespace in DrawingGroups DrawingGroup.ClipGeometry does not affect GetBounds (differs from WPF)

4 participants