WPF renders differently on Windows XP vs Windows 7
Why does WPF render differently on Windows XP vs Windows 7?
I'm using .NET SP1 on both computers..
My layout is like this window that has no toolbar and is set to maximize so it fits the whole screen.
Under that, I have a Viewbox set to use Stretch: Uniform, and under that I have my LayoutRoot.
This way I hoped to get the same layout on all computers, but it seems it doesn't render exactly the same on Windows XP. Some items are a bit smaller and the layout doesn't look that great.
I have tried to change my resoulution on my Windows 7 computer to the same as the Windows XP computer, and it keeps the layout like it is supposed to.
And both computers use 96 DPI.
Wi开发者_如何学运维ndows XP
Windows 7
Took me about three hours to finally figure this out - after much detective work, but now it is pixel perfect!
It appears that WPF on Windows XP and WPF on Windows 7 not only have different default font faces as well as a default font sizes.
- I had a problem where fonts were rendering differently on Windows XP from how they were on Windows 7. It was quite critical since the final output was to the printer, and they needed to be identical. It appeared initially that the problem was a difference in line spacing.
- Yes - I had the same exact font installed on Windows XP as I was using on Windows 7
- Yes - I tried a very generic font (Arial) and still had the same problems.
- Yes - Same DPI on both machines.
- Yes - Same result whether in a VM (Windows XP Mode) or on a real Windows XP machine.
Eventually I discovered that the fonts where I was specifying an explicit size looked identical on Windows XP, and only the ones where I didn't specify an explicit size were they different.
So here's how I fixed it in my MainWindow.xaml
- with a ContentControl
to set a default size:
<Grid x:Name="LayoutRoot" Background="#FFDEDEDE" UseLayoutRounding="True">
<ContentControl FontFamily="Segoe UI" FontSize="12">
... window contents ...
</ContentControl>
</Grid>
Note: If you're using Blend you may need to enter FontSize="12"
by hand. If you select it from the properties designer it will delete it, because it thinks 12 is already the default!
Like I said my destination was the printer - so I had to do the same for the control being printed.
Where else can I set this default font size? Anyhow, I now have pixel perfect rendering on Windows XP and Windows 7, and they differ only by the cleartype anti-aliasing differences.
Note: UseLayoutRounding
is not part of my solution - but I always use it on my root control also.
The default fonts are different
Make a WPF button
<Button x:Name="button" Width="100" Height="25" Content="Button" Click="Button_Click"/>
and code behind:
private void Button_Click(object sender, RoutedEventArgs e)
{
string msg = string.Format("Number of fonts: {1}{0}Font Family: {2}{0}Font Size: {3}",
Environment.NewLine,
button.FontFamily.FamilyNames.Values.Count.ToString(),
button.FontFamily.FamilyNames.Values.First().ToString(),
button.FontSize.ToString());
MessageBox.Show(msg);
}
Run this on each operating system and you will see that the default fonts for XP and Windows7 are different.
Default font for XP is “Tahoma” size 11
Default font for Windows 7 is “Segoe UI” size 12
My experience:
I'm not sure if it is the issue, I noticed Windows 7 uses hardware acceleration to draw the WPF application. Windows XP doesn't.
You can check if this is the case by using something like this:
public partial class App
{
public static int Tier { get { return RenderCapability.Tier >> 16; } }
static App()
{
Console.Out.WriteLine("Render Tier: {0}", Tier);
}
}
Your rendering tier should return 2 if it used full hardware accelerated drawing. 0 = software, 1 = something in the middle if guess
Different versions of Windows have different default fonts (also different versions of the same fonts) and different font rendering engines - as a result the text size is different between systems.
You can try to set the font to the same font and see how it works, maybe try several fonts to check where the difference is smallest.
精彩评论