c# - 即使启用了 ClearTypeHinting,WPF 中的文本也很模糊?

标签 c# .net wpf xaml cleartype

我在 WPF/XAML 中有一个包含此模板和样式的网格:

<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
            <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ContentPresenter x:Name="CellContent" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RenderOptions.ClearTypeHint="Enabled" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="CellContent" Property="TextOptions.TextFormattingMode" Value="Display" />
                    <Setter TargetName="CellContent" Property="RenderOptions.ClearTypeHint" Value="Enabled" />
                    <Setter TargetName="CellContent" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect ShadowDepth="2" BlurRadius="2" Color="Black" RenderingBias="Quality" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

DropShadowEffect 当您选择一个网格行时,似乎使文本渲染变得模糊(灰色抗锯齿):

enter image description here

当我移除投影效果时,它看起来很清晰,因为它现在使用 ClearType 而不是灰色子像素抗锯齿:

enter image description here

我已经尝试将 RenderOptions.ClearTypeHint="Enabled" 应用于 ContentPresenter ,如上所示,但没有帮助。

我如何强制 WPF 呈现使用投影效果显示的文本以保留 Cleartype 抗锯齿,而不是那种丑陋模糊的灰色子像素抗锯齿?

有些人认为它模糊是因为投影 - 这是不正确的。它模糊只是因为没有使用 ClearType。这是在 Firefox 中使用阴影和 ClearType 时的样子:

enter image description here

启用 ClearType 的文本是彩色的——但模糊文本不是,因为它不使用 ClearType——它使用灰色子像素抗锯齿,而这不是 ClearType 的工作方式:http://en.wikipedia.org/wiki/ClearType

问题是:如何为该文本启用 ClearType?

最佳答案

如何将 TextOptions.TextFormattingMode 设置为 Display 以及将 RenderOptions.BitmapScalingMode 设置为 NearestNeighbor?后者是 WPF 3.5 SP1 中的新功能,我通常用它来消除模糊。 :)

<TextBlock Text="Hello world" TextOptions.TextFormattingMode="Display"
           RenderOptions.BitmapScalingMode="NearestNeighbor"
           HorizontalAlignment="Center" TextWrapping="Wrap"
           VerticalAlignment="Center" Foreground="White" FontFamily="Microsoft Sans Serif">
    <TextBlock.Effect>
        <DropShadowEffect ShadowDepth="2" BlurRadius="2" Color="Black"
                          RenderingBias="Quality"/>
    </TextBlock.Effect>
</TextBlock>

下面是它的样子。

enter image description here

这就是它在 FireFox 中的样子。

enter image description here

关于c# - 即使启用了 ClearTypeHinting,WPF 中的文本也很模糊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161200/

相关文章:

c# - 解析 XML 并保存到对象中

c# - x64 应用程序访问 mdb 数据库

c# - 在 Silverlight 中打开文件夹对话框?

C# WPF项目,如何避免InvalidOperationException?

c# - Xamarin - 多目标 - "' System.ValueTuple` 2' is declared in multiple referenced"

c# - 如何提取 XML 文件中出现的每个标签

java - 将数据从 SQL Server 传输到 Hadoop

c# - 使用 CaSTLe ActiveRecord,当两个类具有相同名称但命名空间不同时,我得到一个 NHibernate DuplicateMappingException

使用 Galasoft MVVMLight 库从 .NET Framework 4.7 迁移到 .NET 5

wpf - HttpClient 上传进度条