question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The problem of region clipping occurs when VisualBrush and Rotatetransform are used together

See original GitHub issue
  • .NET Core Version:.NET 6
  • Windows version: Windows 11
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description: I want to implement a tiled and rotated watermark effect in wpf But when I use VisualBrush and RotateTransform, the excess area is clipped

Actual behavior: 【VisualBrush without RotateTransform】

image

【VisualBrush with RotateTransform】 image

Expected behavior: I found a html demo like this: image

or this: image

Minimal repro: xaml code


<Grid ClipToBounds="False">
        <Grid.Background>
            <VisualBrush
                Stretch="None"
                TileMode="Tile"
                Viewport="0,0,100,50"
                ViewportUnits="Absolute">
                <VisualBrush.Visual>
                    <TextBox
                        Width="100"
                        Height="50"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        VerticalContentAlignment="Center"
                        Background="Blue"
                        Foreground="White"
                        Text="Hello .NET 6">
                        <TextBox.RenderTransform>
                            <RotateTransform Angle="45" CenterX="50" CenterY="25" />
                        </TextBox.RenderTransform>
                    </TextBox>
                </VisualBrush.Visual>
            </VisualBrush>
        </Grid.Background>
    </Grid>

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BigHeadDevcommented, Sep 15, 2022

Two problem:

  1. You should use LayoutTransform instead of RenderTransform
  2. Your VisualBrush.Viewport limit the width and height that is the main reasons for clipping

The fixed code:

        <Grid ClipToBounds="False">
            <Grid.Background>
                <VisualBrush
                    Stretch="None"
                    TileMode="Tile"
                    Viewport="0,0,0.2,0.2"
                    ViewportUnits="RelativeToBoundingBox">
                    <VisualBrush.Visual>
                        <TextBox
                            Width="100"
                            Height="50"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            VerticalContentAlignment="Center"
                            Background="Blue"
                            Foreground="White"
                            Text="Hello .NET 6">
                            <TextBox.LayoutTransform>
                                <RotateTransform Angle="15" CenterX="50" CenterY="25" />
                            </TextBox.LayoutTransform>
                        </TextBox>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Grid.Background>
        </Grid>

The demo code: https://github.com/lindexi/lindexi_gd/tree/fa7dfe687c00baaf083c875afa36d0b223a972b8/DakajoheachuNercairrihijal

Thank you for your reply.

I found that your code can only work properly under specific circumstances, but if The Grid’s width and height isn‘t auto,or TextBox’s angle is bigger than 15 will not take effect~ like this:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Width="200">
            <Slider
                x:Name="sliderAngle"
                Maximum="180"
                Minimum="0" />
            <TextBox x:Name="input" Text="Hello .Net 6" />
        </StackPanel>

        <Grid
            Grid.Row="1"
            Width="1000"
            Height="300"
            ClipToBounds="False">
            <Grid.Background>
                <VisualBrush
                    Stretch="None"
                    TileMode="Tile"
                    Viewport="0,0,0.2,0.2"
                    ViewportUnits="RelativeToBoundingBox">
                    <VisualBrush.Visual>
                        <TextBox
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            VerticalContentAlignment="Center"
                            Background="Blue"
                            Foreground="White"
                            Text="{Binding ElementName=input, Path=Text, UpdateSourceTrigger=PropertyChanged}">
                            <TextBox.LayoutTransform>
                                <RotateTransform Angle="{Binding ElementName=sliderAngle, Path=Value}" CenterX="50" CenterY="25" />
                            </TextBox.LayoutTransform>
                        </TextBox>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Grid.Background>
        </Grid>

    </Grid>

actually, in my actual development process, the angle, the Width and Height of the Grid, and the content of the TextBox can change dynamically

Sorry, this may be a complicated requirement, and I hope it can be solved

(by the way 林德熙大佬, I often see your articles, ha ha😁)

1reaction
lindexicommented, Sep 15, 2022

Two problem:

  1. You should use LayoutTransform instead of RenderTransform
  2. Your VisualBrush.Viewport limit the width and height that is the main reasons for clipping

The fixed code:

        <Grid ClipToBounds="False">
            <Grid.Background>
                <VisualBrush
                    Stretch="None"
                    TileMode="Tile"
                    Viewport="0,0,0.2,0.2"
                    ViewportUnits="RelativeToBoundingBox">
                    <VisualBrush.Visual>
                        <TextBox
                            Width="100"
                            Height="50"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            VerticalContentAlignment="Center"
                            Background="Blue"
                            Foreground="White"
                            Text="Hello .NET 6">
                            <TextBox.LayoutTransform>
                                <RotateTransform Angle="15" CenterX="50" CenterY="25" />
                            </TextBox.LayoutTransform>
                        </TextBox>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Grid.Background>
        </Grid>

The demo code: https://github.com/lindexi/lindexi_gd/tree/fa7dfe687c00baaf083c875afa36d0b223a972b8/DakajoheachuNercairrihijal

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF clip rectangle with path not getting desired output
I want to clip water rectangle in a way such that it only displayed in container. I have tried clipping using path geometry...
Read more >
Mitigating Airspace Issues In WPF Applications
Painting operations are clipped to the visible regions of ancestors. This means that child windows cannot paint outside of their parent windows.
Read more >
WPF Interview Questions And Answers
This article lists the most asked WPF Interview questions and answers. What is WPF? How to learn WPF? WPF tutorials.
Read more >
How to: Use Clipping with a Region - Windows Forms .NET ...
The region is passed to the SetClip method of a Graphics object, and then two strings are drawn. The following illustration shows the...
Read more >
WPF and Silverlight Super-Productivity: ListBoxes
The clipping region is a simple rectangle with a width based on that rating. That particular clipping rectangle is created by a binding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found