1

There is a criticism of one of my applications. The displayed text is perceived as blurry. I zoomed the window somewhat and got this result (so they are right)

Blurry text

resulting from what I've tried here in this simplified example:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBlock Text="StatusDTC [1]"></TextBlock>
            <TextBlock Text="StatusDTC [2]"
                       RenderOptions.BitmapScalingMode="NearestNeighbor"
                       RenderOptions.EdgeMode="Aliased"></TextBlock>
            <TextBlock Text="StatusDTC [3]" 
                       SnapsToDevicePixels="True"></TextBlock>
            <TextBlock Text="StatusDTC [4]"
                       SnapsToDevicePixels="True"
                       RenderOptions.BitmapScalingMode="NearestNeighbor"
                       RenderOptions.EdgeMode="Aliased"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

I found something similar here. but UseLayoutRounding seems to be not available for .Net 3.5. I googled a bit and found that there are improvements in .Net 4 (TextOptions.TextRenderingMode) addressing this issue, but switching to .Net 4 with this application is not an option.

As I know that Win7 renders WPF different than XP, I also started a virtual XP and tried it there. The result is the same.

Does someone have an idea to crisp the text in .Net 3.5?

Community
  • 1
  • 1
Markus
  • 4,487
  • 3
  • 41
  • 51
  • see also this article: http://stackoverflow.com/questions/190344/wpf-blurry-fonts-problem-solutions (mostly 4.0) – fixagon Feb 05 '13 at 08:30

3 Answers3

3

I don't think short of using a Bitmap font you can do anything here. :( BitmapScalingMode should not affect your current example as long as the font you are using is Vector based, you really do want the 4.0 text improvements

In Windows 7, the text drawing was switched to DWrite and that's why it's different than XP.

Ameen
  • 2,576
  • 1
  • 14
  • 17
  • 1
    Just to confirm, the relatively blurry text in WPF was a long standing issue that only got fixed in .NET 4.0. If you want WPF to render your text, then the only way to get it looking as crisp as normal Windows text is to use v4.0 (which, to be fair, shipped almost 3 years ago!). – Ian Griffiths Feb 05 '13 at 15:16
  • @IanGriffiths Sure, .Net4 is not something brand new. But some things are a company decision and therefore out of my area of influence :-/ Thanks for making my dead-end position that crystal. Now I can stop searching and this is helpful too. – Markus Feb 06 '13 at 08:03
2

If upgrading to .NET 4.0 is absolutely not an option, there's one last (slightly desperate) option you could consider: get something else to render the text. For example, you could use, say, GDI+ to render the text you need to a bitmap and then display that. Or you could use interop to host a Windows Forms Label control.

These are both truly horrible solutions (which is why I've only suggested this after you confirmed in the comments that using a recent version of WPF is simply not an option). The interop one will lead to all the usual HWND interop problems (i.e, the Label will get its own HWND, meaning that it will be rendered completely separately from the WPF content, which might lead to obvious visual discontinuities.)

So if I were stuck in this situation I think I'd look at the option of rendering to a bitmap. It's possible to use GDI+ to produce a bitmap which you can then render with a WPF Image element. And if you were prepared to write a custom control to do this you could even support things like data binding (by defining a Text dependency property for the text you render). It's not straightforward though - although there is common ground in terms of support for Windows bitmaps, transferring the data from the GDI+ world to WIC (Windows Imaging Components, which is what WPF relies on for its bitmap handling) is...messy. Also, if you have any need to support accessibility (e.g., making the text visible to screen readers, supporting mnemonic access keys etc.) then this becomes a relatively complex undertaking.

Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
  • Thank you, good list of theoretical possibilities. In a bound DataGrid (from the WPF Toolkit, because 3.5) with several hundred entries and numerous columns I will venture here probably no attempts. For others here, this may be a way. – Markus Feb 06 '13 at 10:50
1

As explained in the link from Ameen's answer, this problem is mostly seen at small font sizes.

Is increasing the font size an option for you? I know that this is more a workaround than a solution, but it's a quick and easy way to get rid of the blurryness if you cannot move away from Framework 3.5.

Heinzi
  • 167,459
  • 57
  • 363
  • 519