WPF 圖形系統(tǒng)使用與設(shè)備無(wú)關(guān)的單元來(lái)支持分辨率和設(shè)備無(wú)關(guān)性。每個(gè)與設(shè)備無(wú)關(guān)的像素都會(huì)隨系統(tǒng)上的每英寸點(diǎn)數(shù) (dpi) 設(shè)置自動(dòng)縮放。這支持 WPF 應(yīng)用程序可以根據(jù)不同的 dpi 設(shè)置進(jìn)行適當(dāng)?shù)目s放,并使應(yīng)用程序可以自動(dòng)識(shí)別 dpi。
但是,這種 dpi 無(wú)關(guān)性可能會(huì)因?yàn)橄忼X而呈現(xiàn)出不規(guī)則的邊緣。當(dāng)邊緣的位置處于設(shè)備像素內(nèi)而不是位于設(shè)備像素之間時(shí),可出現(xiàn)這些通常顯示為模糊邊緣或半透明的效果邊緣。為解決此問(wèn)題,WPF 提供了一種通過(guò)像素對(duì)齊將可視化樹(shù)中的對(duì)象邊緣與設(shè)備像素對(duì)齊或固定到設(shè)備像素的方法,從而消除了因消除鋸齒而產(chǎn)生的半透明邊緣。
像素對(duì)齊是一種通過(guò)對(duì)可見(jiàn)幾何圖形應(yīng)用較小的偏移量以將幾何圖形與設(shè)備像素對(duì)齊、從而消除視覺(jué)偽影效果的方法。
本主題包括下列各節(jié)。
<Page x:Class="PixelSnapping.Images" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Images" > <StackPanel> <!-- Image has a pixel dimension of 144x96. --> <!-- Because the image has a even width and height, an even border width and height allows the image to proper center. --> <Border HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="100"> <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="sharpness.png" Stretch="None"/> </Border> <!-- Image has a pixel dimension of 144x96. --> <!-- Because the image has a even width and height, an odd border width and height causes the image to soften. --> <Border HorizontalAlignment="Left" VerticalAlignment="Top" Width="201" Height="101"> <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="sharpness.png" Stretch="None"/> </Border> </StackPanel> <!-- Grid Background --> <Page.Background> <DrawingBrush Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile"> <DrawingBrush.Drawing> <DrawingGroup> <GeometryDrawing Brush="White"> <GeometryDrawing.Geometry> <RectangleGeometry Rect="0,0,1,1" /> </GeometryDrawing.Geometry> </GeometryDrawing> <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z " Brush="#CCCCFF" /> <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#CCCCFF" /> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> </Page.Background></Page>
遺憾的是,僅調(diào)整容器對(duì)象的大小并不能保證設(shè)備像素的對(duì)齊。應(yīng)用程序的整個(gè)布局可以對(duì)圖像的對(duì)齊產(chǎn)生影響。顯示每英寸點(diǎn)數(shù) (dpi) 也會(huì)影響圖像的對(duì)齊。在以上示例中,僅在將顯示設(shè)置為 96 每英寸點(diǎn)數(shù) (dpi) 時(shí),圖像對(duì)齊才起作用。在其他設(shè)置中,需要調(diào)整布局以適應(yīng)顯示設(shè)置。在 Windows Presentation Foundation (WPF) 應(yīng)用程序中,應(yīng)盡量避免高頻圖像。