开发者

Why does UseLayoutRounding not seem to work with Viewbox?

开发者 https://www.devze.com 2022-12-13 04:58 出处:网络
I\'m trying to write a tile-based game in WPF 4. I want the game board to scale to fit the window, so I\'m using a Viewbox; but I want each tile to be on a nice, crisp pixel boundary. I might be wrong

I'm trying to write a tile-based game in WPF 4. I want the game board to scale to fit the window, so I'm using a Viewbox; but I want each tile to be on a nice, crisp pixel boundary. I might be wrong, but my understanding is that this is what the new UseLayoutRounding property is supposed to be for -- but it isn't working the way I expect.

Here's a window that demonstrates the issue:

<Window x:Class="Tyler.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        x开发者_JS百科mlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="600" Height="400" Background="Black">
    <Viewbox>
        <Canvas Width="1000" Height="1000">
            <Rectangle Canvas.Left="100" Canvas.Top="100"
                       Width="100" Height="100" Fill="Gray"/>
            <Rectangle Canvas.Left="200" Canvas.Top="100"
                       Width="100" Height="100" Fill="Gray"/>
        </Canvas>
    </Viewbox>
</Window>

The two rectangles are adjacent, but because of the sub-pixel coordinates (resulting from the Viewbox's scaling), I end up with a darker gray seam between them. That's what I'm trying to get rid of -- I want them to blend together seamlessly.

But UseLayoutRounding doesn't seem to have this effect. I've tried setting UseLayoutRounding="True" on the Window, the Viewbox, the Canvas, the Rectangles -- I've even tried putting it on all of them at once. There's no effect on the seam.

What am I missing (or misunderstanding)? How can I get layout rounding to work with a Viewbox?


SnapsToDevicePixels=True is what you want

<Viewbox SnapsToDevicePixels="True" >
    <Canvas Width="1000" Height="1000">
        <Rectangle Canvas.Left="100" Canvas.Top="100"
                   Width="100" Height="100" Fill="Gray"/>
        <Rectangle Canvas.Left="200" Canvas.Top="100"
                   Width="100" Height="100" Fill="Gray"/>
    </Canvas>
</Viewbox>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号