WPF 小文本渲染和缩放
当我使用画布缩放时,我需要“稳定”应用程序中的文本渲染。
我有以下代码:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="356" Width="804">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer
PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" Grid.Row="0">
<Canvas>
<Canvas.LayoutTransform>
<TransformGroup>
<ScaleTransform x:Name="scaleTransform"/>
</TransformGroup>
</Canvas.LayoutTransform>
<TextBlock Canvas.Left="34" Canvas.Top="47" Height="23" Name="textBlock1" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, Aliased" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Aliased" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="34" Canvas.Top="81" Height="23" Name="textBlock2" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, ClearType" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="35" Canvas.Top="115" Height="23" Name="textBlock3" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, Grayscale" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Grayscale" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="310" Canvas.Top="46" Height="23" Name="textBlock4" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, Aliased" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="Aliased" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="309" Canvas.Top="79" Height="23" Name="textBlock5" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, ClearType" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="309" Canvas.Top="112" Height="23" Name="textBlock6" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, Grayscale" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="Grayscale" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="188" Canvas.Top="157" FontSize="11" Height="23" Name="textBlock11" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default" FontFamily="Times New Roman" FontWeight="Bold" />
</Canvas>
</ScrollViewer>
<TextBlock Grid.Row="1" Height="23" Name="textBlock8" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default, static; Times New Roman, 11" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Grid.Row="2" FontSize="11" Height="23" Name="textBlock9" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; DISPLAY, static; Times New Roman, 11" TextOptions.TextFormattingMode="Display" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Grid.Row="3" Height="23" Name="textBlock7" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default, static; Arial, 16" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Row="4" FontSize="16" Height="23" Name="textBlock10" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; DISPLAY, static; Arial, 16" TextOptions.TextFormattingMode="Display" FontWeight="Bold" />
</Grid>
</Window>
当您将其粘贴到 Visual Studio 中时,在设计器中按 Ctrl 并滚动,您将看到默认的 15 尺寸以下是模糊的。
如果我缩放画布,TextFormattingMode
的选项 DISPLAY
就会被破坏。
使用 ScaleTransform 时如何避免这种小文本模糊问题?
I need to "stabilize" the text rendering in an application when I use canvas scaling.
I have the following code:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="356" Width="804">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer
PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" Grid.Row="0">
<Canvas>
<Canvas.LayoutTransform>
<TransformGroup>
<ScaleTransform x:Name="scaleTransform"/>
</TransformGroup>
</Canvas.LayoutTransform>
<TextBlock Canvas.Left="34" Canvas.Top="47" Height="23" Name="textBlock1" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, Aliased" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Aliased" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="34" Canvas.Top="81" Height="23" Name="textBlock2" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, ClearType" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="35" Canvas.Top="115" Height="23" Name="textBlock3" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Ideal, Grayscale" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Grayscale" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="310" Canvas.Top="46" Height="23" Name="textBlock4" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, Aliased" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="Aliased" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="309" Canvas.Top="79" Height="23" Name="textBlock5" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, ClearType" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="309" Canvas.Top="112" Height="23" Name="textBlock6" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Display, Grayscale" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="Grayscale" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Canvas.Left="188" Canvas.Top="157" FontSize="11" Height="23" Name="textBlock11" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default" FontFamily="Times New Roman" FontWeight="Bold" />
</Canvas>
</ScrollViewer>
<TextBlock Grid.Row="1" Height="23" Name="textBlock8" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default, static; Times New Roman, 11" FontSize="11" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Grid.Row="2" FontSize="11" Height="23" Name="textBlock9" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; DISPLAY, static; Times New Roman, 11" TextOptions.TextFormattingMode="Display" FontFamily="Times New Roman" FontWeight="Bold" />
<TextBlock Grid.Row="3" Height="23" Name="textBlock7" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; Default, static; Arial, 16" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Row="4" FontSize="16" Height="23" Name="textBlock10" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; DISPLAY, static; Arial, 16" TextOptions.TextFormattingMode="Display" FontWeight="Bold" />
</Grid>
</Window>
When you paste it in the Visual Studio, press Ctrl and Scroll in designer, you will see, that the default under 15 size is blurry.
And the option DISPLAY
of the TextFormattingMode
does is broken if I zoom the canvas.
How to avoid this small text blurriness problem when using ScaleTransform
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以尝试缩放字体大小,而不是在画布上使用缩放变换。不过,让文本对齐可能很困难。按照您的方式缩放画布将强制进行一些插值,并且您将无法避免模糊的文本。
老实说,我没有看到两个屏幕截图之间的模糊度有任何差异。
Rather than using a scaling transform on the canvas, perhaps you could try scaling the font size. It may be hard to get the text to line up though. Scaling the canvas in the manner that you are will force some interpolation to happen, and you will not be able to avoid the blurry text.
To be honest I don't see any difference in blurriness between the two screenshots.