WPF网格像素对齐问题

发布于 2024-08-08 03:51:11 字数 2909 浏览 1 评论 0原文

我正在使用 WPF 网格来对齐对象,并遇到了有关列的像素对齐的相当明显的问题。我尝试删除尽可能多的变量,并设法在这段代码中显示问题:

<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100.5" />
        <ColumnDefinition Width="199.5" />
    </Grid.ColumnDefinitions>
    <Border Background="Red">
        <Grid.Column>1</Grid.Column>
    </Border>
    <Border Background="Red">
    </Border>
</Grid>
</Window>

如果运行该示例,很容易发现两列之间的边框存在问题。我相信,这是因为 WPF 只是将一列与背景进行 alpha 混合,另一列与结果进行 alpha 混合,即使从概念上讲,两列都在相同的 Z 中,因此像素应该是它们的权重和背景。

我知道这是一个有问题的问题,当然我不会故意创建具有部分像素大小的列,但在使用星形大小(我确实经常使用)时可以轻松观察到相同的效果。

可以通过使用 SnapsToDevicePixels 属性( 而不是 )来解决此问题。这是可行的,因为 WPF 具有内部一致的舍入,因此两列都会捕捉到同一像素。 然而,我遇到了一个与此非常相似的相关问题,但我找不到解决方案。

由于某种原因,当使用 Geometry 类时,我遇到了同样类型的像素对齐问题,而这次我没有找到任何解决方法。就好像像素是圆形的,但现在几何体被折断了一个像素,留下了一个 1 像素宽的孔。

示例代码:

<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid SnapsToDevicePixels="False">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100.5" />
        <ColumnDefinition Width="199.5" />
    </Grid.ColumnDefinitions>
    <Border Background="Red">
        <Grid.Column>1</Grid.Column>
    </Border>
    <Border>
        <Border.Background>
            <DrawingBrush>
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="Red">
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Border.Background>
    </Border>
</Grid>
</Window>

知道如何正确对齐我的像素吗?

编辑:

根据答案添加 GuidelineSet 后现在可以正常工作。工作图代码为:

<DrawingGroup>
    <DrawingGroup.GuidelineSet>
        <GuidelineSet GuidelinesX="0,1" GuidelinesY="0,1"></GuidelineSet>
    </DrawingGroup.GuidelineSet>                
    <GeometryDrawing Brush="Red">
        <GeometryDrawing.Geometry>
            <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry>
        </GeometryDrawing.Geometry>
    </GeometryDrawing>
</DrawingGroup>

I'm using the WPF Grid to align objects, and ran into a rather glaring issue regarding pixel alignment of columns. I tried to remove as many variables as possible, and managed to show the issue in this code:

<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100.5" />
        <ColumnDefinition Width="199.5" />
    </Grid.ColumnDefinitions>
    <Border Background="Red">
        <Grid.Column>1</Grid.Column>
    </Border>
    <Border Background="Red">
    </Border>
</Grid>
</Window>

If you run that sample, it's easy to see that there's an issue in the border between the two columns. It's caused, I believe, because WPF simply alpha-blends one column with the background and the other with the result, even though conceptually both columns are in the same Z, so the pixel should be the blending of the sum of their weights and the background.

I understand this is a problematic issue, and naturally I don't intentionally create columns with partial pixel sizes, but the same effect can be easily observed when using star sizes (which I do use a lot).

This issue can be worked around by using the SnapsToDevicePixels property (<Grid SnapsToDevicePixels="True"> instead of <Grid>). this works because WPF has internally-consistent rounding, so both columns snap to the same pixel.
However, I hit a related problem which is very similar in vein, to which I couldn't find a solution.

For some reason, when using the Geometry class, I get the same sort of pixel alignment issues, and I this time I didn't find any sort of work-around. It's as if the pixels are rounded, but now the Geometry is snapped one pixel off, leaving a hole 1-pixel-wide hole.

Example code:

<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid SnapsToDevicePixels="False">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100.5" />
        <ColumnDefinition Width="199.5" />
    </Grid.ColumnDefinitions>
    <Border Background="Red">
        <Grid.Column>1</Grid.Column>
    </Border>
    <Border>
        <Border.Background>
            <DrawingBrush>
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="Red">
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Border.Background>
    </Border>
</Grid>
</Window>

Any idea how to get my pixels aligned properly?

EDIT:

Working correctly now after adding a GuidelineSet according to the answer. Working drawing code is:

<DrawingGroup>
    <DrawingGroup.GuidelineSet>
        <GuidelineSet GuidelinesX="0,1" GuidelinesY="0,1"></GuidelineSet>
    </DrawingGroup.GuidelineSet>                
    <GeometryDrawing Brush="Red">
        <GeometryDrawing.Geometry>
            <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry>
        </GeometryDrawing.Geometry>
    </GeometryDrawing>
</DrawingGroup>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-08-15 03:51:11

我相信您必须在绘图的情况下使用GuidelineSet。 SDK 中的此处提供了有关如何应用 GuidelineSet 的很好的部分。

I believe you must use a GuidelineSet in the case of a Drawing. There's a good section on how to apply GuidelineSets available here in the SDK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文