3

I created a simple web browser WPF test application with pictures and text within a canvas, with windows set at 96dpi.

Then I switched to 120 dpi and :-((( Display is messy, image size changed and part of the canvas is out of view...

When I used Winforms, I set the AutoScaleMode property to None and the windows keeps its size, the controls as well, the controls which have inherited font are properly displayed, not blurry and not too big...

What can I do to mimic this (good) behavior in W¨F?

Didier Levy
  • 3,393
  • 9
  • 35
  • 57

3 Answers3

1

I just found a bug using MaxHeight under WPF in .NET 4, set in a Style that gets inherited by another Style and that is used as a StaticResource, which didn't get influenced by the DPI set by the user. I set it from MaxHeight to Height, then it got influenced by the DPI. I suspect a bug in the .NET 4 (and possibly other frameworks) here.

Akku
  • 4,373
  • 4
  • 48
  • 67
1

I'm not clear on what you mean by "web browser WPF ... application". WPF doesn't run in a Web browser, unless you're talking about an XBAP. Or are you doing Silverlight? Or is it just a WPF navigation application and not browser-based at all? You'll need to clarify.

WPF automatically scales your content when you run in high-DPI modes. This is intended behavior: if the user explicitly says they want everything to be bigger on the screen, then WPF will respect the user's wishes. The old WinForms hacks of "pretend high-DPI doesn't exist, just show everything at the normal small size and hope it doesn't piss the user off too much" aren't available in WPF; you could probably emulate them if you worked at it, but you're steered very strongly toward doing the Right Thing.

WPF scales everything, so your statement that "part of the canvas is out of view" doesn't make sense. It should be scaling the canvas, its parent window, and its child elements all by the same amount, so if everything fits at 96dpi, it should also fit at 120dpi and 144dpi. If not, then you're doing something strange and you'll have to provide a code sample that reproduces the problem.

You seem to be claiming that fonts are blurry when you run in a high-DPI mode, which sounds very strange. Fonts are rendered as vectors, so they should scale cleanly, and render crisply even in high-DPI modes. I've never seen the blurry fonts you describe, so again, you'll have to provide a repro case.

The only thing that I would expect to be blurry are images. If you're using raster (bitmap) images (BMP / GIF / JPG / PNG) in your UI -- for example, for the icons on a toolbar -- then yes, those will look pretty bad when they're scaled. It pretty much always looks bad when you take a small bitmap and make it larger. You might try working around this by using larger images and sizing them down for display -- for example, if you want your toolbar images to be 16x16 (when in standard 96-dpi mode), then you could try putting a 32x32 bitmap in your project, setting the Image element's Width="16" and Height="16" in your XAML, and seeing if that looks any better. It would actually be 20x20 physical pixels in 120dpi mode, and 24x24 in 144dpi mode, both of which would still be scaled down from the 32x32 resource and would therefore have a better shot of looking good than a 16x16 source image that's had to be scaled up. (I haven't tried this technique in a WPF toolbar, though, so I don't know how well it would really work in practice with typical toolbar images.)

The very best way to get around the problems with scaling images would be to use vector images instead of raster. Unfortunately, it's hard to find libraries of vector images. They're few, far between, typically less comprehensive than what you can find for bitmap images, and often expensive.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • Joe, I'm talking xbap. Though I'm fully aware that ALL is redrawn according to 120 or 144 dpi in WPF. Sorry for the blurry font, you're true, those are letters encapsulated within bitmap images... My bad! That being said, when you draw an application at 96 dpi that fits the whole scren, if another user is on 144 dpi, he'll hardly see the halp of you application. So the question is: does he have to use scrolbars or do resize the whole stuff? – Didier Levy May 08 '11 at 11:14
  • So you're trying to hard-code an XBAP to exactly fit on, say, a 1024x768 screen? You're fighting an uphill battle. Different users will have different browser versions with different toolbars and other chrome taking up different amounts of vertical space. They'll have different Windows settings (titlebar height, taskbar height, even which side of the monitor the taskbar is docked to). Not to mention different screen resolutions and aspect ratios (e.g. widescreen monitors that are wider and not as tall). You're better off using WPF's layout features to design a UI that resizes gracefully. – Joe White May 08 '11 at 13:31
1

Presumably you use fixed length units (px). Try re-layouting your project keeping the WPF layout rules in mind. This page has some best practices for that.

seri
  • 324
  • 2
  • 11